Sending SMTP Email with Advanced Features in Visual Basic.NET | Part 2 – Include Alternate eMail Views

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 3.80 out of 5)
 

This is Part 2 of the Sending Email using SMTP with Advanced Features series. The first series went over how to add multiple attachments to an email message for someone to download when they view your email. I suggest you check that article out at this web link. Also for the basics of sending Smtp emails you really should check out the article here.

This article will show how to send your email message with Alternative Views. For instance, you can send an HTML based email message and also include a Plain Text Only version in the same email. This can help ensure that the recipient(s) can view your email in case the email provider or client software only supports Text based emails and so on. All you need to do is make your html based message and the plain text only message, then add those views to the email message using the AlternateView class.

Note: I do want to mention that these series of articles only work with Visual Basic 2005, VB 2008, Visual Basic 2010 and Higher since I am using the class libraries under the System.NET.Mail namespace, which wasn’t added until .NET Framework 2.0 and higher which of course includes version 3.5, DotNet 4.0, and newer.

First of all to get started you will want an instance of a couple classes. I am using the same gmail based codes I used in the first advanced article for this series.

  • System.Net.Mail.SmtpClient
  • System.Net.Mail.MailMessage


Here starts the code.

'The Simple Mail Tranfer Protocol client with the Host and Port number to use. You will
'want to change these settings to what you need to use. The host, smtp.gmail.com and port
'587 will work if you use a gmail account.
Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)

'This will contain the actual message data to send.
Dim eMailmessage As New System.Net.Mail.MailMessage

'Used to setup the eMail as an html view.
Dim htmlView As AlternateView

'Used to setup the mail to have a plain text view.
Dim plainTextView As AlternateView

'Will temporarily hold the text for the html message view.
Dim theHtmlMessage As String

'This variable will temporarily hold the plain text format view.
Dim thePlainMessage As String

 

Now you need to setup your credentials and the Secure Sockets Layer (SSL) property. The Secure Sockets Layer, or Ssl property, is to be set to True for many smtp (Simple Mail Transfer Protocol) providers. It is required for Googles Gmail, Microsofts Hotmail, Office Live Mail, and even Yahoo mail.


'Use Secure Socket Layer to Encrypt the connection for sending the mail. This needs
'be set to "True".
smtp.EnableSsl = True

'Setup the gmail host.
smtp.Credentials = New System.Net.NetworkCredential("gmailLoginName@gmail.com", "gmailLoginPassword")

 

Now setup the basics of your email like the recipient(s), subject, and so on…


eMailmessage.Subject = "Check out my email message!"

eMailmessage.From = New Mail.MailAddress("you@hotmail.com, "Jason")

eMailmessage.To.Add("ToPerson@yahoo.com)

 

The next code is the contents of your email message. One will be the body content for the html format view, and the other is the contents for the text based view.


theHtmlMessage = "<strong>This is Html!</strong><br/><br/><u>This is Html as well!</u>"

thePlainMessage = "This is only plain text! No html formatting!"

 

Now that both the plain text and html message is in a string variable its time to add them to the AlternateView Class as a string itself using the CreateAlternateViewFromString function.


        'Setup the mail message as an html view.
        Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(theHtmlMessage, _
            Nothing, MediaTypeNames.Text.Html)

        eMailmessage.AlternateViews.Add(htmlView)

        'Setup the mail message as an plain text view.
        Dim plainTextView As AlternateView = AlternateView.CreateAlternateViewFromString(thePlainMessage, _
            Nothing, MediaTypeNames.Text.Plain)

            eMailmessage.AlternateViews.Add(plainTextView)

 

As you can see, its quite easy to setup your mail message to have available alternate views. Just simply use the CreateAlternateViewFromString function and pass your message as html in one view and plain text in the other view. Now all thats really left is to send your email message to the recipient that you specified in the earlier source code.


'When you are done with setting up your email and your alternate view, then you just need
'to send your message.
smtp.Send(eMailmessage)


Thats all there is to it! So, after its all said and done, go ahead and get quite creative with your email messages using html, but always remember that it would be a good idea to also create a basic text email in case the persons mail client doesn’t support html so the recipients can still view your email message. Thats all for Part 2. I’m not exactly sure about Part 3 but it will probably go over sending your email messages to muliple recipients and carbon copys (cc) to other recipients as well.

Have Fun!

Jason

Revised: 2014

3 thoughts on “Sending SMTP Email with Advanced Features in Visual Basic.NET | Part 2 – Include Alternate eMail Views

  1. Pilm

    Didn’t see it listed but need to import System.Net.Mime for MediaTypeNames. BTW, I was looking for RTF, and even though not shown above, you clued me into how to do it and where to find it… thanks!

    Reply
  2. Jason Post author

    Hi Pilm, your right about the Imports statement for the mime types.

    I should have also mentioned in the article that there is a nice rtf to html class made with vb.net at codeproject.com. It will convert the richtextbox controls: text, data, formatting, and so on to html source code to make it look just like it did in the richtext control for your html emails, webpages, and so on.

    I think that is what you were referring to about the rtf info? You can check out the link below for the class if you want. I used it for an email example I made that I put up at my vbcodesource.com website. It makes it quite easy to send an email that is formatted and setup in a richtextbox control. 🙂

    http://www.codeproject.com/Articles/16219/Using-a-VB-NET-2005-Class-to-Convert-RichText-to-H

    Jason

    Reply
  3. Ron Chandler

    I want to send an email with a .jpg attached, not embedded. It was possible using CDO, but I cannot find out how to do it using SMTP!

    Reply

Leave a Reply