Sending eMails with Visual Basic with LinkedResources and Alternate Views


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)


         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:

  • LinkedResource
  • AlternateView

Embedding Pictures Inside the Body of an Email

   Private Sub SendEmail()
         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")

         ' Embed image to body of email
         Dim objLinkedRes As New LinkedResource("Image.png", _
         objLinkedRes.ContentId = "Image"
         Dim objHTLMAltView As AlternateView = _
            AlternateView.CreateAlternateViewFromString("<img src='cid:Image' _
            />", New System.Net.Mime.ContentType("text/html"))

         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")

      Catch ex As Exception


      End Try

   End Sub

   Sub Main(
   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!

This article was originally published on May 6th, 2016

About the Author

Hannes DuPreez

Hannes du Preez is a self-taught developer who started learning to program in the days of QBasic. He has written several articles over the years detailing his programming quests and adventures. .NET is his second love, just after his wife and kid. He has always been an avid supporter of .NET since the beginning and is an expert in VB and C#. He was given the Microsoft Most Valuable Professional Award for .NET (2008–2017). He has worked as a moderator and an article reviewer on online forums and currently works as a C# developer and writes articles for CodeGuru.com, Developer.com, DevX.com, and the Database journal.
His first book Visual Studio 2019 In Depth is currently on sale on Amazon and Bpb Publications.

You could reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date