The example uses an aspx page as well as a code-behind page. We will first look at the aspx page. Notice the opening tag of the panel control near the top of the program. Its properties should be pretty much self explanatory. The closing tag for the panel is near the bottom of the page. Notice also the enctype attribute on the <form> line. The enctype is necessary because we are using the htmlinput control to locate attachments. HtmlInput is normally used for uploading files from your file system to a server. Here, however, we are using only its browsing component to locate attachments you may want to include.
The rest of the page are simple TextBoxes to hold the elements of an email such as From (which needs to be in email format), To, Subject, etc. A button is also included to initiate the send operation. The code-behind page is mainly the click event code for the submit button.
<html> <body> <h3>Email with .NET</h3> <asp:Panel ID="panel1" Runat="Server" Font-Name="Verdana" BorderStyle="Ridge" BorderColor="MidnightBlue" BackColor="Ivory" width="50%"> <form id="Form1" runat="server" enctype="multipart/form-data"> <table> <tr> <td align="right">From:</td> <td align="left"><asp:TextBox ID="txtFrom" Runat="Server" /></td> </tr> <tr> <td align="right">To:</td> <td align="left"><asp:TextBox ID="txtTo" Runat="Server" /></td> </tr> <tr> <td align="right">Subject:</td> <td align="left"><asp:TextBox ID="txtSubject" Runat="Server" /></td> </tr> <tr> <td align="right">Message Body:</td> <td align="left"> <asp:TextBox ID="txtMessage" TextMode="MultiLine" rows="6" Runat="Server" /> </td> </tr> <tr> <td align="right">Priority:</td> <td align="left"> <asp:DropDownList ID="ddlPriority" Runat="Server"> <asp:ListItem>Low</asp:ListItem> <asp:ListItem>Normal</asp:ListItem> <asp:ListItem>High</asp:ListItem> </asp:DropDownList> <td> </tr> <tr> <td align="right">CC:</td> <td align="left"><asp:TextBox ID="txtCC" Runat="Server" /></td> </tr> <tr> <td align="right">BCC:</td> <td align="left"><asp:TextBox ID="txtBCC" Runat="Server" /></td> </tr> <tr> <td align="right">Select File to Attach:</td> <td align="left"> <INPUT type="file" id="txtAttachment" runat="server" NAME="txtAttachment"> </td> </tr> <tr> <td colspan="2" align="center"> <asp:Button ID="btnSend" Text="Send" Type="Submit" OnClick="btnSend_Click" runat="Server" /> </td> </tr> </table> </form> </asp:Panel> </body> </html> |
Imports System Imports System.Web.Mail Imports System.Web Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.HtmlControls Imports System.IO |
Public Class SendEmail : Inherits Page Protected txtFrom As TextBox Protected txtTo As TextBox Protected txtSubject As TextBox Protected txtMessage As TextBox Protected ddlPriority As DropDownList Protected txtCC As TextBox Protected txtBCC As TextBox Protected txtAttachment As HtmlInputFile |
Public Sub btnSend_Click(Sender As Object, e As EventArgs) Dim objMail As New MailMessage Dim objConn As SmtpMail Dim objAttach As MailAttachment Dim postedFile = txtAttachment.PostedFile Dim strPath As String = "" Try strPath = Path.GetFullPath(postedFile.FileName) Catch End Try |
objMail.From = txtFrom.Text objMail.To = txtTo.Text objMail.Subject = txtSubject.Text objMail.Body = txtMessage.Text If ddlPriority.SelectedItem.Text = "Low" Then objMail.Priority = MailPriority.Low ElseIf ddlPriority.SelectedItem.Text = "Normal" Then objMail.Priority = MailPriority.Normal Else objMail.Priority = MailPriority.High End If objMail.Cc = txtCC.Text objMail.Bcc = TxtBCC.Text Try objAttach = New MailAttachment(strPath) objMail.Attachments.Add(objAttach) Catch End Try objConn.Send(objMail) End Sub End Class |
本文地址:http://com.8s8s.com/it/it43726.htm