0

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?

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by jonniebl
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.