Using the MailMessage Object

   

Using the MailMessage Object

An alternative way of sending an e-mail is to use the MailMessage object. After the object is created, you need only to provide all the necessary properties of the object to complete the e-mail and send it. Table 15.2 shows the properties of the MailMessage object with a brief description for each one.

Table 15.2. MailMessage Properties

Property

Description

From

The e-mail address of the sender.

  To  

The e-mail address of the recipient.

Subject

The subject of the e-mail.

  CC  

Recipients to be copied on the e-mail.

BCC

Recipients to be blind-copied on the e-mail.

Priority

Priority of the e-mail message: Low, High, or Normal.

BodyEncoding

Type of encoding for the e-mail: Base64 or UUEncode .

BodyFormat

Type of formatting for the e-mail: plain text or HTML.

Attachments

A list of MailAttachment objects.

URLContentBase

All relative URLs used within an HTML-encoded message.

Headers

The dictionary of custom headers to be sent with the e-mail. This is a read-only property.

Now let's look at how to create and send an e-mail using the MailMessage object with its basic elements.

First, create a form to capture all the information and populate the MailMessage object. Finally, the MailMessage object is passed to the Send() method of the SmtpMail object to be sent. Let's look at an example of creating the MailMessage object in Listing 15.3 that shows how to use it to send an e-mail.

Let's first look at a VB example.

Listing 15.3 Visual Basic Example
 <%@ Import Namespace="System.Web.Mail" %>  <HTML>  <head>  <script language="VB" id="Script1" runat="server">  Sub SendMyEmail (Obj As Object, E As EventArgs)    Dim mailObj AS new MailMessage    mailObj.From = Request.Form("FromAddress")    mailObj.To = Request.Form("ToAddress")    mailObj.Subject = Request.Form("Subject")    mailObj.Body = Request.Form("Message")    SmtpMail.Send(mailObj)  End Sub  </SCRIPT>  </head>   <body>  <form METHOD="post" RUNAT="server">  <table>      <tr>          <td width="50"><font face="Arial" size="1">From:</td>          <td width=400><asp:textbox  width="400" id="FromAddress"              runat="Server"></asp:textbox></td>      </tr>      <tr>          <td><font face="Arial" size="1">To:</td>          <td><asp:textbox  width="400" id="ToAddress" runat="Server">            </asp:textbox></td>      </tr>      <tr>          <td><font face="Arial" size="1">Subject:</td>          <td><asp:textbox  width="400" id="Subject" runat="Server">             </asp:textbox></td>      </tr>      <tr>          <td colspan=2><asp:textbox width="450" height="100" id="Message"            runat="Server"></asp:textbox></td>      </tr>      <tr>          <td colspan=2 align="center"><asp:button id="button1" text="Send"             runat="Server" onclick="SendMyEmail"></asp:button> </td>      </tr>  </table>  </form>  </BODY>  </HTML> 

Next, let's look at a C# example in Listing 15.4.

Listing 15.4 A C# Example
 <%@ Import Namespace="System.Web.Mail" %>  <HTML>  <head>  <script language="C#" id="Script1" runat="server">  void SendMyEmail (Object obj, EventArgs e)  {    MailMessage mailObj = new MailMessage();    mailObj.From = Request.Form["FromAddress"];    mailObj.To = Request.Form["ToAddress"];    mailObj.Subject = Request.Form["Subject"];    mailObj.Body = Request.Form["Message"];    SmtpMail.Send(mailObj);  }  </SCRIPT>  </head>   <body>  <form METHOD="post" RUNAT="server">  <table>      <tr>          <th></th><th><font face="Arial" size="2">              Email example using the MailMessage object</font></td>      </tr>      <tr>          <td width="50"><font face="Arial" size="1">From:</td>          <td width=400><asp:textbox  width="400" id="FromAddress"             runat="Server"></asp:textbox></td>      </tr>      <tr>          <td><font face="Arial" size="1">To:</td>          <td><asp:textbox  width="400" id="ToAddress" runat="Server">             </asp:textbox></td>      </tr>      <tr>          <td><font face="Arial" size="1">Subject:</td>          <td><asp:textbox  width="400" id="Subject" runat="Server">             </asp:textbox></td>      </tr>      <tr>          <td colspan=2><asp:textbox width="450" height="100" id="Message"            runat="Server"></asp:textbox></td>      </tr>      <tr>          <td colspan=2 align="center"><asp:button id="button1" text="Send"             runat="Server" onclick="SendMyEmail"></asp:button> </td>      </tr>  </table>  </form>  </BODY>  </HTML> 

The precedingexample constructed a simple form to collect e-mail fields, which include From, To, Subject Line, and Message Body. After all the information is filled out, the Submit button calls SendMyEmail() and captures all the information from the form into the MailMessage object. Last, the overloaded Send() method of the SmptMail object is used to send the message.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net