System.Net.Mail FAQ

Complete FAQ Listing


2.1 MailMessage Class Printer Friendly


Click here for MSDN docs

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

 

[ C# ]
 static void PlainText()
{
//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.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);
}


[ VB.NET ]
 Sub PlainText()
'create the mail message
Dim mail As 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."

'send the message
Dim smtp As New SmtpClient("127.0.0.1")
smtp.Send(mail)
End Sub 'PlainText