Smtp Classes in .Net

MailMessage Class:

     The MailMessage class can be considered the foundation class of the System.Net.Mail namespace. It deals with creating and managing the email message. All other classes will somehow interact with this class. The MailMessage class exposes such properties as the

Property

 Description

Attachments

 Gets the attachment collection used to store data attached to this e-mail message.

Bcc

 Gets the address collection that contains the blind carbon copy (BCC) recipients for this e-mail message.

Body

 Gets or sets the message body.

CC

 Gets the address collection that contains the carbon copy (CC) recipients for this e-mail message.

From

 Gets or sets the from address for this e-mail message.

Subject

 Gets or sets the subject line for this e-mail message.

To

 Gets the address collection that contains the recipients of this e-mail message.

Below you will find an example of using the MailMessage class

Example:

//create the mail message
MailMessage mail = new MailMessage ();
//set the addresses
mail.From = new MailAddress ("me@mycompany.com");
mail.To.Add ("you@yourcompany.com");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email”;

MailAddress Class:

            The MailAddress class is used for creating email addresses. This class is used for setting the MailMessage.From, MailMessage.To, MailMessage.CC and MailMessage.BCC properties. Of these properties the .From class is actually a MailAddress, while the To, CC and BCC properties are actually collections of MailAddresses. The two most common properties of the MailAddress class are the DisplayName and the Address properties. They are described below.

Name

Description

Address

Gets the e-mail address specified when this instance was created.

DisplayName

Gets the display name composed from the display name and address information specified when this instance was created.

Example:

//set the addresses

//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress ("me@mycompany.com", "Steve James");

Attachment Class:

            The Attachment class is used for creating and managing individual attachments of the MailMessage object. Attachments can be created from streams or file paths. The stream or file path must be set in the ctor of the Attachment.

Below is an example demonstrating the Attachment class

[C#]

 Static void AttachmentFromFile ()

{

//create the mail message

MailMessage mail = new MailMessage ();

//set the addresses

mail.From = new MailAddress (“me@mycompany.com”);

mail.To.Add (“you@yourcompany.com”);

//set the content

mail.Subject = “This is an email”;

mail.Body = “this content is in the body”;

 

//add an attachment from the file system

mail.Attachments.Add (new Attachment (“c:\\temp\\example.txt”));

//to add additional attachments, simply call .Add (…) again

mail.Attachments.Add (new Attachment (“c:\\temp\\example2.txt”));

mail.Attachments.Add (new Attachment (“c:\\temp\\example3.txt”));

//send the message

SmtpClient smtp = new SmtpClient(“127.0.0.1”);

smtp.Send(mail);

 

}

SmtpClient Class:

The SmtpClient class is responsible for sending or transporting the email. The SmtpClient can transport the email content over the network, or it can actually write them to the filesystem in the MS IIS Smtp Service Pickup Directory format, which resembles a RFC821 formatted message. Emails can be sent either synchronously or asynchronously. The SmtpClient also supports sending email via SSL for security purposes. The following list of properties are the most common used on the SmtpClient class.

Name

Description

Credentials

Gets or sets the credentials used to authenticate the sender.

DeliveryMethod

Specifies how outgoing email messages will be handled.

EnableSsl

Specify whether the SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.

Host

Gets or sets the name or IP address of the host used for SMTP transactions.

Port

Gets or sets the port used for SMTP transactions.

 

AlternateView class:

The AlternateView class is used for providing alternate bodies and creating Multi-Part mime emails. If you want to create an email that will be rendered properly in both Html capable and Plain Text only mail clients, then you will create alternate views of the message. There are a few main properties and methods you will use with the AlternateView class. They are:

Name

Description

BaseUri

Gets or sets the Base URI to use for resolving relative URIs in the AlternateView

LinkedResources

Gets the set of embedded resources referred to by this attachment.

CreateAlternateViewFromString (static method)

Overloaded. Creates an AlternateView to view an email message using the specified format..

 

LinkedResource class:

The LinkedResource class is the last, and least used main class. It is mainly used for creating embedded images. To create an embedded image you will need to first create a Html formatted AlternateView. Within that alternate view you create an tag, that points to the ContentId (CID) of the LinkedResource. You then create a LinkedResource object and add it to the AlternateView’s LinkedResources collection.

Advertisements

One response

  1. Congratulations, you know I have a problem with the class LinkedResource trying to embed an image by passing the route, but when I send email gmail ami, if the message but the picture is not the only thing you see is the icon of the image, (know that may be happening? hope you can help me, I leave you my email: mascodigo.net @ hotmail.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: