[Sample of Mar 23th] Use SMTP to send emails

 

Homepage image
Sample of the Day RSS Feed

Sample Downloads
C# version:
https://code.msdn.microsoft.com/CSSMTPSendEmail-2717a609
VB version: https://code.msdn.microsoft.com/VBSMTPSendEmail-bdcec608 

Today’s code sample demonstrates sending email with attachment and embedded image in the message body using SMTP server from a C# or VB.NET program. 

imageYou can find more code samples that demonstrate the most typical programming scenarios by using Microsoft All-In-One Code Framework Sample Browser or Sample Browser Visual Studio extension. They give you the flexibility to search samples, download samples on demand, manage the downloaded samples in a centralized place, and automatically be notified about sample updates. If it is the first time that you hear about Microsoft All-In-One Code Framework, please watch the introduction video on Microsoft Showcase, or read the introduction on our homepage https://1code.codeplex.com/.

 

Introduction

CS/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.To.Add("anyreceiver@anydomain.com") 
     mail.From = New MailAddress(anyaddress@anydomain.com) 

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; 
    htmlView.LinkedResources.Add(imgLink); 
    mail.AlternateViews.Add(htmlView); 

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 https://www.ietf.org.

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.live.com" 
    smtp.Credentials = New NetworkCredential("myaccount@live.com", "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.

     smtp.Send(mail) 

 

More Information

MSDN: SmtpClient Class
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Sending Emails from C# Application using default SMTP
https://www.codeproject.com/KB/cs/Sending_Mails_From_C_.aspx