Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

MailMessage Class

Note: This API is now obsolete.

Provides properties and methods for constructing an e-mail message. Recommended alternative: System.Net.Mail.


Namespace:  System.Web.Mail
Assembly:  System.Web (in System.Web.dll)
[ObsoleteAttribute("The recommended alternative is System.Net.Mail.MailMessage. http://go.microsoft.com/fwlink/?linkid=14202")]
public class MailMessage

The MailMessage type exposes the following members.

Public methodMailMessageInitializes a new instance of the MailMessage class. Recommended alternative: System.Net.Mail.
Public propertyAttachmentsSpecifies the collection of attachments that are transmitted with the message. Recommended alternative: System.Net.Mail.
Public propertyBccGets or sets a semicolon-delimited list of email addresses that receive a blind carbon copy (BCC) of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyBodyGets or sets the body of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyBodyEncodingGets or sets the encoding type of the body of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyBodyFormatGets or sets the content type of the body of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyCcGets or sets a semicolon-delimited list of e-mail addresses that receive a carbon copy (CC) of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyFieldsGets a collection of objects that map to Microsoft Collaboration Data Objects (CDO) fields. Recommended alternative: System.Net.Mail.
Public propertyFromGets or sets the e-mail address of the sender. Recommended alternative: System.Net.Mail.
Public propertyHeadersSpecifies the custom headers that are transmitted with the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyPriorityGets or sets the priority of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertySubjectGets or sets the subject line of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyToGets or sets a semicolon-delimited list of recipient e-mail addresses. Recommended alternative: System.Net.Mail.
Public propertyUrlContentBaseGets or sets the Content-Base HTTP header, the URL base of all relative URLs used within the HTML-encoded body of the e-mail message. Recommended alternative: System.Net.Mail.
Public propertyUrlContentLocationGets or sets the Content-Location HTTP header for the e-mail message. Recommended alternative: System.Net.Mail.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The following example shows how to use the MailMessage class.

Security noteSecurity Note

This control has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

This example shows how to send a mail message from a Web Forms page
using the classes in the System.Web.Mail namespace.

<%@ IMPORT namespace="System.Web.Mail" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script language="C#" runat="server">
  void Page_Load()
     if (!IsPostBack)
        txtBody.Text="This is a test message.";
        txtAttach.Text=@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Sunset.jpg,"
           + @"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg";
    txtBodyEncoding.Text = Encoding.ASCII.EncodingName;
        // Name of relay mail server in your domain.

  void btnSubmit_Click(Object sender, EventArgs e)
     string sTo, sFrom, sSubject, sBody;
     string sAttach, sCc, sBcc, sBodyEncoding;
     string sBodyFormat, sMailServer, sPriority;
     string sUrlContentBase, sUrlContentLocation;
 int iLoop1;

 sTo = txtTo.Text.Trim();
 sFrom = txtFrom.Text.Trim();
 sSubject = txtSubject.Text.Trim();
 sBody = txtBody.Text.Trim();
 sAttach = txtAttach.Text.Trim();
 sCc = txtCc.Text.Trim();
 sBcc = txtBcc.Text.Trim();
 sBodyFormat = txtBodyFormat.Text.Trim();
 sBodyEncoding = txtBodyEncoding.Text.Trim();
 sPriority = txtPriority.Text.Trim();
 sUrlContentBase = txtUrlContentBase.Text.Trim();
 sUrlContentLocation = txtUrlContentLocation.Text.Trim();
 sMailServer = txtMailServer.Text.Trim();

 MailMessage MyMail = new MailMessage();
 MyMail.From = sFrom;
 MyMail.To = sTo;
 MyMail.Subject = sSubject;
 MyMail.Body = sBody;
 MyMail.Cc = sCc;
 MyMail.Bcc = sBcc;
 MyMail.UrlContentBase = sUrlContentBase;
 MyMail.UrlContentLocation = sUrlContentLocation;

     if (txtBodyEncoding.Text == Encoding.UTF7.EncodingName)
        MyMail.BodyEncoding = Encoding.UTF7;
     else if (txtBodyEncoding.Text == Encoding.UTF8.EncodingName)
        MyMail.BodyEncoding = Encoding.UTF8;
        MyMail.BodyEncoding = Encoding.ASCII;

 switch (sBodyFormat.ToUpper())
        case "HTML": 
           MyMail.BodyFormat = MailFormat.Html;
           MyMail.BodyFormat = MailFormat.Text;

     switch (sPriority.ToUpper())
        case "HIGH": 
           MyMail.Priority = MailPriority.High;
        case "LOW": 
           MyMail.Priority = MailPriority.Low;
           MyMail.Priority = MailPriority.Normal;

     // Build an IList of mail attachments.
     if (sAttach != "")
        char[] delim = new char[] {','};
        foreach (string sSubstr in sAttach.Split(delim))
           MailAttachment MyAttachment = new MailAttachment(sSubstr);

     SmtpMail.SmtpServer = sMailServer;
 lblMsg1.Text="C# Message sent to " + MyMail.To;

  void btnClear_Click(Object sender, EventArgs e)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Mail Form Example</title>

   <h4>Send a new mail message:</h4>
   <form id="form1" method="Post" action="MailForm.aspx" runat="server">
      <table style="width:350; background-color:#FFFF99">
            <td align="Right"><b>To:</b></td>
            <td><Asp:Textbox id="txtTo" runat="server"/></td>
            <td align="Right"><b>From:</b></td>
            <td><Asp:Textbox id="txtFrom" runat="server"/></td>
            <td align="Right"><b>Subject:</b></td>
            <td><Asp:Textbox id="txtSubject" runat="server"/></td>
            <td align="Right"><b>MessageBody:</b></td>
            <td><Asp:Textbox id="txtBody" runat="server"/></td>
            <td align="Right"><b>Attachments:</b></td>
            <td><Asp:Textbox id="txtAttach" runat="server"/></td>
            <td align="Right"><b>CC:</b></td>
            <td><Asp:Textbox id="txtBcc" runat="server"/></td>
            <td align="Right"><b>BCC:</b></td>
            <td><Asp:Textbox id="txtCc" runat="server"/></td>
            <td align="Right"><b>BodyEncoding:</b></td>
            <td><Asp:Textbox id="txtBodyEncoding" runat="server"/></td>
            <td align="Right"><b>BodyFormat:</b></td>
            <td><Asp:Textbox id="txtBodyFormat" runat="server"/></td>
            <td align="Right"><b>Priority:</b></td>
            <td><Asp:Textbox id="txtPriority" runat="server"/></td>
            <td align="Right"><b>URL Content Base:</b></td>
            <td><Asp:Textbox id="txtUrlContentBase" runat="server"/></td>
            <td align="Right"><b>URL Content Location:</b></td>
            <td><Asp:Textbox id="txtUrlContentLocation" runat="server"/></td>
            <td align="Right"><b>Mail Server:</b></td>
            <td><Asp:Textbox id="txtMailServer" runat="server"/></td>
      </table><br />

      <asp:button id="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" runat="server"/>
      <asp:button id="btnClear" Text="Clear" OnClick="btnClear_Click" runat="server"/>
      <p><asp:Label id="lblMsg1" runat="server"/></p>

.NET Framework

Supported in: 1.1, 1.0
Obsolete (compiler warning) in 4.5.1
Obsolete (compiler warning) in 4.5
Obsolete (compiler warning) in 4
Obsolete (compiler warning) in 3.5
Obsolete (compiler warning) in 3.5 SP1
Obsolete (compiler warning) in 3.0
Obsolete (compiler warning) in 3.0 SP1
Obsolete (compiler warning) in 3.0 SP2
Obsolete (compiler warning) in 2.0
Obsolete (compiler warning) in 2.0 SP1
Obsolete (compiler warning) in 2.0 SP2

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.