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.
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("firstname.lastname@example.org, "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.