MailMessage Class
Assembly: System (in system.dll)
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.
To specify the sender, recipient, and contents of an e-mail message, use the associated properties of the MailMessage class.
| Mail part | Property |
|---|---|
| Sender | |
| Recipient | |
| Carbon copies (CC) | |
| Blind carbon copies (BCC) | |
| Attachments | |
| Subject | |
| Message body |
Use the AlternateViews property to specify copies of an e-mail message in different formats. For example, if you send a message in HTML, you might also want to provide a plain text version in case some of the recipients use e-mail readers that cannot display HTML content. For an example that demonstrates creating a message with alternate views, see AlternateViews.
After assembling your e-mail message, you can send it by using the Send or SendAsync methods.
The following code example demonstrates creating and sending an e-mail message that includes an attachment.
public static void CreateMessageWithAttachment(string server) { // Specify the file to be attached and sent. // This example assumes that a file named Data.xls exists in the // current working directory. string file = "data.xls"; // Create a message and set up the recipients. MailMessage message = new MailMessage( "jane@contoso.com", "ben@contoso.com", "Quarterly data report.", "See the attached spreadsheet."); // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); // Add the file attachment to this e-mail message. message.Attachments.Add(data); //Send the message. SmtpClient client = new SmtpClient(server); // Add credentials if the SMTP server requires them. client.Credentials = CredentialCache.DefaultNetworkCredentials; client.Send(message); // Display the values in the ContentDisposition for the attachment. ContentDisposition cd = data.ContentDisposition; Console.WriteLine("Content disposition"); Console.WriteLine(cd.ToString()); Console.WriteLine("File {0}", cd.FileName); Console.WriteLine("Size {0}", cd.Size); Console.WriteLine("Creation {0}", cd.CreationDate); Console.WriteLine("Modification {0}", cd.ModificationDate); Console.WriteLine("Read {0}", cd.ReadDate); Console.WriteLine("Inline {0}", cd.Inline); Console.WriteLine("Parameters: {0}", cd.Parameters.Count); foreach (DictionaryEntry d in cd.Parameters) { Console.WriteLine("{0} = {1}", d.Key, d.Value); } data.Dispose(); }
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.You still have the ability to override the default body content transfer encoding settings of the MailMessage class, instead of setting the body properties of the mail message object set the body property to null, Then add an alternate view with the body and the desired content transfer encooding. You still cannot use 8bit encoding but you now have the option to override what the MailMessage thinks your message should be.
message.Body = null
using (AlternateView body = AlternateView.CreateAlternateViewFromString("My 7bit encoded Text message", message.BodyEncoding, message.IsBodyHtml ? "text/html" : null))
{
body.TransferEncoding = TransferEncoding.SevenBit;
message.AlternateViews.Add(body);
SmtpClient client = new SmtpClient(Host, Port);
client.Send(message);
}
- 9/27/2007
- Abstract Labs
- 7/22/2008
- Stanley Roark
- 7/11/2007
- bellwa
Note that System.Net.Mail is not available in .NET 1.1 framework. One will not be able to use this in Visual Studio 2003 or earlier.
This namespace is available in .NET framework 2.0 or higher.
- 4/7/2007
- Ashish Basran
- 4/7/2007
- Ashish Basran
if in a sended message body you'll find sequence =0D=0A and you email client can't get it right, you can do this:
change content-transfer-encoding - by default in .net 2.0 it's quoted-printable, in old .net it was 8bit.
if you think it's gonna work simply setting header like this emailMessage.Headers.Set("content-transfer-encoding", "8bit") you're sadly mistaken, .NET will add one more time content-transfer-encoding, and will add default one.
So you need to act in a wrong way - simply use old obsolete class: System.Web.Mail.MailMessage
Finding out this took some time for me :(
- 3/14/2007
- Giedrius Banaitis
- 3/14/2007
- Giedrius Banaitis
Adapting the code above to the using() pattern:
using(MailMessage message = new MailMessage("jane@contoso.com","ben@contoso.com","report","See attachment."))
using(Attachment data = new Attachment(file, MediaTypeNames.Application.Octet))
{
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
/* REST OF CODE GOES HERE */
}
// what about the namespace for ContentDisposition ?
// 1) of no consequence to this example 2) google, do you use it? 3) take questions to forums or newsgroups, not here
This ensures that Dispose is called on both the MailMessage and the Attachment when the executing thread exits the method.
- 1/19/2007
- Will Sullivan
- 2/27/2007
- Will Sullivan
Failing to call AttachmentBase.Dispose will result in locks being held on any files that were sent as attachments.
Calling MailMessage.Dispose is a way to ensure that any attachments will also be disposed.
- 1/19/2007
- Jonathan Allen