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:

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

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

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

Add additional emails like so:
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;'>" &_


            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)
            End If

            Dim emailClient As New SmtpClient

Last update:
2014-08-14 23:44
Average rating:0 (0 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

Records in this category