The .NET Framework supplies classes for sending emails. All of these use the same technology (SMTP), but each may have some drawbacks. Today, I will not only demonstrate how to send emails through Visual Basic, but to make them more powerful with the use of LinkedResources and Alternate Views Classes.
SMTP (Simple Mail Transfer Protocol) is an Internet standard for email transmission. The Simple Mail Transfer Protocol is a text-based protocol and is connection oriented. With SMTP, a mail sender communicates with a mail receiver by issuing commands over a data stream channel such as TCP (Transmission Control Protocol), for example.
SMTP sessions consist of a few commands initiated by the SMTP client as well as a few corresponding responses from the SMTP server. These commands are the following:
- MAIL: Establishes the return path, or rather, the return address.
- RCPT: Establishes the recipient of the message.
- DATA: Identifies the beginning of the message's content. This contains the message header as well as the message body.
Let's move on to some of the classes that can be used to send emails.
The SmtpClient Class
The SmtpClient class is used to send emails to SMTP servers for delivery.
The following information must be specified to construct and send an email message by using SmtpClient:
- The SMTP host server that you use to send email.
- Credentials for authentication.
- The email address of the sender.
- The email address (or addresses) of the recipient(s).
- The message content.
- Optional: The message attachment. To include an attachment with an email, you have to first create the attachment by using the Attachment class, and then add it to the message by using the MailMessage.Attachments property.
Here is a short implementation of the SmtpClient class:
Imports System.Net.Mail Imports System.ComponentModel Namespace SmtpClientExample Public Class smtpClientExample Public Shared Sub Main(ByVal args() As String) Dim sClient As New SmtpClient(args(0)) Dim maFrom As New MailAddress("UserFrom@FromAddress.com", _ "From User", System.Text.Encoding.UTF8) Dim maTo As New MailAddress("Userto@ToAddress.com") Dim mmMessage As New MailMessage(maFrom, maTo) mmMessage.Body = "TEST EMAIL" mmMessage.Body & Environment.NewLine mmMessage.BodyEncoding = System.Text.Encoding.UTF8 mmMessage.Subject = "Test Message" mmMessage.SubjectEncoding = System.Text.Encoding.UTF8 Dim strUserState As String = "Test Message" sClient.SendAsync(mmMessage, strUserState) mmMessage.Dispose() Console.WriteLine("Email Sent Successfully") End Sub End Class End Namespace
With the preceding code segment, I have kept it very, very simple. I have created a new SmtpClient object. Then, I constructed the email piece by piece and used the SmtpClient class to send the constructed email.
Although the SmtpClient class forms part of the System.Net.Mail namespace, I have decided rather to explain it with SMTP. Now that we know much more about how emails work in Visual Basic, we can start playing around with them!
The System.Net.Mail Namespace
The System.Net.Mail namespace contains all classes necessary to send email to an SMTP server for delivery. For a comprehensive list of all the various classes inside the System.Net.Mail namespace, follow this MSDN article.
Inside the abovementioned link, look closely at the following two classes; you will use them to add some power to ordinary emails:
Embedding Pictures Inside the Body of an Email
Private Sub SendEmail() Try Dim client As New SmtpClient("smtp.SMTPSERVER.com") client.UseDefaultCredentials = False Dim message As New MailMessage() message.IsBodyHtml = True Dim TEST As New MailAddress("emailaddressTo") message.To.Add(TEST) ' Embed image to body of email Dim objLinkedRes As New LinkedResource("Image.png", _ "image/png") objLinkedRes.ContentId = "Image" Dim objHTLMAltView As AlternateView = _ AlternateView.CreateAlternateViewFromString("<img src='cid:Image' _ />", New System.Net.Mime.ContentType("text/html")) objHTLMAltView.LinkedResources.Add(objLinkedRes) message.AlternateViews.Add(objHTLMAltView) Dim messageFrom As New MailAddress("emailaddressfrom") message.From = messageFrom Dim messageSender As New MailAddress("emailaddressfrom") message.Sender = messageSender message.Subject = "Funky email example" Dim messageAttachment As New Attachment("FileToAttach") message.Attachments.Add(messageAttachment) client.Send(message) Catch ex As Exception Console.WriteLine(ex.ToString()) End Try End Sub Sub Main( SendEmail() End Sub
In the preceding example, I made use of the LinkedResource class to identify the object, in this case a picture, and to embed it inside the body via the help of the AlternateView class. For an added bonus, I quickly just showed how you could attach a separate file to your email as well.
By using the SmtpClient class together with the LinkedResource and AlternateView classes, you make great, professional emails. Until next time, cheers!