Creating E-Mail Messages

   

In this section, we'll talk about two ways to send e-mail messages. The first is simple, the second is more complicated but offers additional flexibility.

Creating Simple E-Mail Messages

Creating an e-mail is quite simple using the SmptMail object. Because the Send() method of the object is overloaded, it can take several variations of information. You can send an e-mail in two basic ways: pass all the components of an e-mail to the object separately, or pass it a MailMessage object. First look at passing separate components of an e-mail to the Send method by studying Listing 15.1.

Let's first look at a VB example.

Listing 15.1 Visual Basic Example to Send Mail
 <%@ Import Namespace="System.Web.Mail" %>  <HTML>  <head>  <script language="VB" id="Script1" runat="server">   Sub SendMyEmail (Obj As Object, E As EventArgs)    Dim ToAddress as String    Dim Subject as String    Dim Body as String    FromAddress = Request.Form("FromAddress")    ToAddress = Request.Form("ToAddress")    Subject = Request.Form("Subject")    Body = Request.Form("Message")    SmtpMail.Send(FromAddress,ToAddress,Subject,Body)  End Sub  </SCRIPT>  </head>  <body>  <form METHOD="post" RUNAT="server">  <table>      <tr>          <th colspan="2"><font face="Arial" size="2">               Simple email example</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"              TextMode="MultiLine" 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> 

Now let's look at a C# example in Listing 15.2.

Listing 15.2 A C# Example
 <%@ Import Namespace="System.Web.Mail" %>  <HTML>  <head>  <script language="C#" id="Script1" runat="server">  void SendMyEmail (Object obj, EventArgs e)  {     String FromAddress;     String ToAddress;     String Subject;     String Message;     FromAddress = Request.Form["FromAddress"];     ToAddress = Request.Form["ToAddress"];     Subject = Request.Form["Subject"];     Message = Request.Form["Message"];     SmtpMail.Send(FromAddress,ToAddress,Subject,Message);  }  </SCRIPT>  </head>  <body>  <form METHOD="post" RUNAT="server" name="EmailForm">  <table>      <tr>          <th colspan="2"><font face="Arial" size="2">             Simple email example</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> 

As you can see from the example, the construction of an e-mail message is simple, and it is also easy to send when you are using this method.

Creating Complex E-Mail Messages

You should notice that the Send() method of the SmptMail object does not return any values about the success of sending the e-mail. This happens because the e-mail is placed in the pickup folder of the InetPub directory. From there, the SMTP service reads and sends the message. If an e-mail were to encounter an error on delivery, the failed attempt would be written to the Badmail folder in the InetPub directory.

As mentioned before, the Send() method of the SmptMail object is overloaded, so a second way to call the Send() method is to pass a reference to the MailMessage object, which leads to the next section. You can see the demo application running in Figure 15.1.

Figure 15.1. Sending an e-mail using a simple form and the SmtpMail object.

graphics/15fig01.gif

   


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