Using the MailMessage ObjectAn 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
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. |