Tag Archives: smtp

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

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

Continue reading

Sending eMail using SMTP in Visual Basic.NET & Visual Basic 2008, VB 2010, and higher

This small article is just going to cover the Normal basic features of an email client to send mail messages. This post shows how to Send a email using Visual Basic 2005, VB 2008, VB.NET 2010, and newer.

Edit: I suggest you also check out the other articles i’ve make that expands on this article about sending your email messages with advanced features. Check out this one, and another one will be done shortly.

 Note: I do want to thank the author of an article at The Code Project for which I got the info for using gMail as the smtp host. 🙂


  1. Attachment
  2. To
  3. From
  4. Subject
  5. Body

To send a mail message through .NET 05, VB 08, Visual Basic 2010  you will want to head to the System.Net.Mail Namespace which contains the important Classes for setting up and sending mail messages. The main class to use is the SMTPClient class. Other classes that will be used is MailMessage and Attachment. To get started Three(Objects) needs to be made to…

  1. The SMTPClient Class
  2. The MailMessage Class
  3. And the Attachment Class


The following code will first setup the System.Net.Mail.MailMessage Class.

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


The next class is the SMTPClient…

'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.
Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)

The code above has created and setup the SMTP Client. Googles gMail actually requires 587 to be the port number from what I understand. You can leave out the Port number if your SMTP doesn’t require a specific port. Also remember if you are not using the GMail to send your messages then you need to change the Host parameter to the Host you are going to use to send your mails. Example: “mysmtp.host.com”


The final Class is the Attachment class…

        'Will contain the attachment info to send with the message.
        Dim attach As System.Net.Mail.Attachment
        'Setup the mail message using 4x Textbox controls that contains the From/To/Subject/Message-Body info.
        'You just  need to set it up the way you want it to be…
        Message = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text)


The code below will check a attachment to make sure the file exists..

        'Need to make sure the attachment exists before adding it to the message.
        If My.Computer.FileSystem.FileExists(txtAttachment.Text) Then Message.Attachments.Add(attach)


Set the Secure Socket Layer to be ‘TRUE’

        'Use Secure Socket Layer to Encrypt the connection for sending the mail.
        'This needs be set to “True” if you plan on using GMail or Hotmail or Yahoo as your host.
        smtp.EnableSsl = True


Then setup your Credentials for your Login info. (Username and Password)

        'Setup your account information for authorization…
        smtp.Credentials = New System.Net.NetworkCredential("yourUsername@yourmail.com", "password")


Now that the Mail setup is completed. The only thing left to do is Send your message.

            'Send the mail message that was setup.
            'Usually it was successful in sending the message if it makes it to here
            'Otherwise it should throw a error message from the Catch exception below.
            'You need to remember that after this point its up to the Host to do the rest.
            MessageBox.Show("Well, the mail message appears to have been asuccess!", _
                "Successful?",MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch exc As Net.Mail.SmtpException

            MessageBox.Show(exc.StatusCode.ToString, " Something Happened?", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

Well all of this code in the article works just fine for me to send messages with attachments. If using VB.NET 02/03 you may have to do some modification. I don’t think .NET 1.0/1.1 has the System.Net.Mail namespace and class. Please leave a comment if you know if/what is different so I can update this article. Hopefully it does for your too 🙂  Take care…


Minor Revision: 2015