To summarize the ASP.NET’s SmtpEmail functionality, an e-mail form containing all the fields and configurable properties is shown in Listing 15.5. In Figure 15.2, you can see the application running. It allows users to enter the information with which the e-mail will be sent.
Figure 15.2. Sending an e-mail using the MailMessage object.
We’ll start with the Visual Basic version.
Listing 15.5. 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.CC = Request.Form(“CCAddress”)
mailObj.BCC = Request.Form(“BCCAddress”)
mailObj.Subject = Request.Form(“Subject”)
If(Len(Request.Form(“Attachments”)) > 0) Then
mailObj.Attachments.Add(new MailAttachment(Request.Form(“Attachments”)))
End If
select case Request.Form(“Format”)
case “Text”
mailObj.BodyFormat = MailFormat.Text
case “HTML”
mailObj.BodyFormat = MailFormat.HTML
end select
select case Request.Form(“Priority”)
case “Low”
mailObj.Priority = MailPriority.Low
case “Normal”
mailObj.Priority = MailPriority.Normal
case “High”
mailObj.Priority = MailPriority.High
end select
select case Request.Form(“Encoding”)
case “ASCII”
mailObj.BodyEncoding = Encoding.ASCII
case “Unicode”
mailObj.BodyEncoding = Encoding.Unicode
case “UTF7”
mailObj.BodyEncoding = Encoding.UTF7
case “UTF8”
mailObj.BodyEncoding = Encoding.UTF8
end select
mailObj.Body = Request.Form(“Message”)
SmtpMail.Send(mailObj)
End Sub
</SCRIPT>
</head>
<body>
<form METHOD=”post” RUNAT=”server”>
<table>
<tr>
<th></th><th><font face=”Arial” size=”2”>MailMessage full example
</font></td>
</tr>
<tr>
<td width=”75”><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”>CC:</td>
<td><asp:textbox width=”400” id=”CCAddress” runat=”Server”>
</asp:textbox></td>
</tr>
<tr>
<td><font face=”Arial” size=”1”>BCC:</td>
<td><asp:textbox width=”400” id=”BCCAddress” 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><font face=”Arial” size=”1”>Attachments:</td>
<td><asp:textbox width=”400” id=”Attachments” runat=”Server”>
</asp:textbox></td>
</tr>
<tr>
<td><font face=”Arial” size=”1”>Format:</td>
<td><select name=”Format”>
<option value=”Text”>Text</option>
<option value=”HTML”>HTML</option>
</select>
</td>
</tr>
<tr>
<td><font face=”Arial” size=”1”>Priority:</td>
<td><select name=”Priority”>
<option value=”Normal”>Normal</option>
<option value=”High”>High</option>
<option value=”Low”>Low</option>
</select>
</td>
</tr>
<tr>
<td><font face=”Arial” size=”1”>Body Encoding:</td>
<td><select name=”Encoding”>
<option value=”Base64”>ASCII</option>
<option value=”Unicode”>Unicode</option>
<option value=”UTF7”>UTF-7</option>
<option value=”UTF8”>UTF-8</option>
</select>
</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>
The C# version of the program appears in Listing 15.6.