VBSMTPSendEmail demonstrates sending email with attachment and embedded image in the message body using SMTP server from a VB.NET program.

With the introduction of .NET 2.0, the classes for sending email are packed in the System.Net.Mail namespace. In the example, we use the MailMessage and the SmtpClient classes.

Using the Code

1.      Build an email object and set the basic email properties.

Represents an e-mail message that can be sent using the SmtpClient class. Instances of the MailMessage class are used to construct e-mail messages that are transmitted to an SMTP server for delivery using the SmtpClient class.

The sender, recipient, subject, and body of an e-mail message may be specified as parameters when a MailMessage is used to initialize a MailMessage object. These parameters may also be set or accessed using properties on the MailMessage object.

 The primary mail message headers and elements for the message may be set using the following properties of the MailMessage class.

     Dim mail As New MailMessage()
     mail.From = New MailAddress("")


2.      Add an attachment of the email.

    mail.Attachments.Add(New Attachment(attachedFile))


3.      Embed an image in the message body.

    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
    mail.Body, null, "text/html");
    LinkedResource imgLink = new LinkedResource(imgFile, "image/jpg");
    imgLink.ContentId = "image1";
    imgLink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64;


4.      Configure the SMTP client.

Allows applications to send e-mail by using the Simple Mail Transfer Protocol (SMTP). The SmtpClient class is used to send e-mail to an SMTP server for delivery. The SMTP protocol is defined in RFC 2821, which is available at

The classes shown in the following table are used to construct e-mail messages that can be sent using SmtpClient.

    Dim smtp As New SmtpClient()
    smtp.Host = ""
    smtp.Credentials = New NetworkCredential("", "mypassword")
    smtp.EnableSsl = True


5.      Send the email.

Sends the specified message to an SMTP server for delivery. This method blocks while the e-mail is transmitted. You can specify a time-out value using the Timeout property to ensure that the method returns after a specified amount of time elapses.

Before calling this method, the Host and Port properties must be set either through the configuration files by setting the relevant properties, or by passing this information into the SmtpClient(String, Int32) constructor.

You cannot call this method if there is a message being sent asynchronously.

If the SMTP host requires credentials, you must set them before calling this method. To specify credentials, use the UseDefaultCredentials or Credentials properties.

If you receive an SmtpException exception, check the StatusCode property to find the reason the operation failed. The SmtpException can also contain an inner exception that indicates the reason the operation failed.

When sending e-mail using Send to multiple recipients and the SMTP server accepts some recipients as valid and rejects others, Send sends e-mail to the accepted recipients and then a SmtpFailedRecipientsException is thrown. The exception will contain a listing of the recipients that were rejected.



More Information

MSDN: SmtpClient Class

Sending Emails from C# Application using default SMTP