ASP.NET VB.NET - How to add attachments to an email


 'For Email
            Dim smtp As New Net.Mail.SmtpClient()

You have to set the host and port...I'm using constant variables (AppSettings) set in my web.config file that look like this:

<connectionStrings>
        <add name="MyConnectionString" connectionString="Data Source=SHOP;Initial Catalog=XXXXXXX;User ID=sXX;Password=$$$$$$$;Packet Size=512" providerName="System.Data.SqlClient"/>
    </connectionStrings>


            smtp.Host = AppSettings("MAIL_REMOTE_SERVER")
            smtp.Port = AppSettings("MAIL_REMOTE_SERVER_PORT")
            Dim SendFrom As MailAddress = New MailAddress("website@website.com")
            Dim SendTo As MailAddress = New MailAddress("mark0@website.com")

            Dim MyMessage As MailMessage = New MailMessage(SendFrom, SendTo)

Add additional emails like so:
            'MyMessage.To.Add("mark@website.com")
            'MyMessage.To.Add("mark2@website.com")
            'MyMessage.To.Add("mark3@website.com")
Add the subject like so:
            MyMessage.Subject = "Notification: Complaint Received From Customer: " & CustomerGuid & " / " & complaintPersonCompany & ""
            MyMessage.Body = _      

'this can be whatever html you have and what vars you 'need from your front-code...example:

"<htmL><title>Customer Complaint</title><head></head><body><h3>Customer <span style='color:brown;'>" & CustomerGuid & " / " & complaintPersonCompany & "</span>" & _
                            " filed this complaint on <span style='color:brown;'>" & DateReceived & "</span></h3><div style='background-color:#fff000;width:60%;'><span style='width:60%;height:25px;color:red;font-size:22px;'>Customer Complaint</span></div>" & _
                           "<h4>Person Filing Complaint General Information</h4><ul>" & _
                           "<li>Company Name: <span style='color:brown;'>" &_

"</span></li></ul></body></html>"


            MyMessage.IsBodyHtml = True
I first get the images that the user uploaded and put them in a datatable called dtImages with one column called ImageLink. I can then loop through and get the images. You need to download the file and convert to data to be sent using WebClient - give it the location

and then you can attach the file
            'for adding the images as attachments in an email
            If dtImages.Rows.Count > 0 Then
                For Each rw As DataRow In dtImages.Rows
                    Dim fileName As String = Path.GetFileName(rw.Item("ImageLink"))
                    Dim data As Byte() = New WebClient().DownloadData(rw.Item("ImageLink"))
                    Dim attachment As New Attachment(New MemoryStream(data), fileName)
                    MyMessage.Attachments.Add(attachment)
                Next
            End If

            Dim emailClient As New SmtpClient
            smtp.Send(MyMessage)

Last update:
2014-08-14 23:44
Author:
Mark
Revision:
1.1
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags