I am trying to write code to send a mail message with an attached file.

my code works fine if I don't try to attach a file but crashes when I do. I have not been able to figure this out. My code looks like this.
[
Dim mail As New MailMessage()
'set the addresses
mail.From = New MailAddress("xxx@cox.net")
mail.To.Add("xxx@cox.net")
'set the content
mail.Subject = "Article to be submitted."
mail.Body = Message
mail.Attachments("C:\Project\Articles1\Article.txt")
'send the message
Dim smtp As New SmtpClient("smtp.east.cox.net")
smtp.Port = 25
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("xxx", "xxx")
smtp.Send(mail)
]
What am I doing wrong?

What am I doing wrong?

Create first an attachment object and after that add it to the attachments

Dim mail As New MailMessage()
        'set the addresses
        mail.From = New MailAddress("xxx@cox.net")
        mail.To.Add("xxx@cox.net")
        'set the content
        mail.Subject = "Article to be submitted."
        Dim MessageText As String = "Here's the article you asked"
        mail.Body = MessageText ' Do not use Message, it's a type i.e. reserved word in System.Net.Mail namespace
        ' You're missing Add and Attachment Item
        Dim MyAttachment As Attachment = New Attachment("C:\Project\Articles1\Article.txt")
        mail.Attachments.Add(MyAttachment)
        'send the message
        Dim smtp As New SmtpClient("smtp.east.cox.net")
        smtp.Port = 25
        smtp.EnableSsl = True
        smtp.Credentials = New System.Net.NetworkCredential("xxx", "xxx")
        smtp.Send(mail)

HTH

Worked great, Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.