By: John Kilgo
Date: December 10, 2002
Download the code.
Printer Friendly Version
In this example you will see how to send email and one way of sending attachments from within an .aspx file. The text boxes for the various components of the email are wrapped in a panel control, so if you have not used panels, you can learn a little about that .net control.
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
1<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.
2
3The 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.
4
5<html>
6<body>
7<h3>Email with .NET</h3>
8<asp:panel backcolor="Ivory" bordercolor="MidnightBlue" borderstyle="Ridge" font-name="Verdana" id="panel1" runat="Server" width="50%">
9<form enctype="multipart/form-data" id="Form1" runat="server">
10<table>
11<tr>
12<td align="right">From:</td>
13<td align="left"><asp:textbox id="txtFrom" runat="Server"></asp:textbox></td>
14</tr>
15<tr>
16<td align="right">To:</td>
17<td align="left"><asp:textbox id="txtTo" runat="Server"></asp:textbox></td>
18</tr>
19<tr>
20<td align="right">Subject:</td>
21<td align="left"><asp:textbox id="txtSubject" runat="Server"></asp:textbox></td>
22</tr>
23<tr>
24<td align="right">Message Body:</td>
25<td align="left">
26<asp:textbox id="txtMessage" rows="6" runat="Server" textmode="MultiLine"></asp:textbox>
27</td>
28</tr>
29<tr>
30<td align="right">Priority:</td>
31<td align="left">
32<asp:dropdownlist id="ddlPriority" runat="Server">
33<asp:listitem>Low</asp:listitem>
34<asp:listitem>Normal</asp:listitem>
35<asp:listitem>High</asp:listitem>
36</asp:dropdownlist>
37<td>
38</td></td></tr>
39<tr>
40<td align="right">CC:</td>
41<td align="left"><asp:textbox id="txtCC" runat="Server"></asp:textbox></td>
42</tr>
43<tr>
44<td align="right">BCC:</td>
45<td align="left"><asp:textbox id="txtBCC" runat="Server"></asp:textbox></td>
46</tr>
47<tr>
48<td align="right">Select File to Attach:</td>
49<td align="left">
50<input id="txtAttachment" name="txtAttachment" runat="server" type="file"/>
51</td>
52</tr>
53<tr>
54<td align="center" colspan="2">
55<asp:button id="btnSend" onclick="btnSend_Click" runat="Server" text="Send" type="Submit"></asp:button>
56</td>
57</tr>
58</table>
59</form>
60</asp:panel>
61</body>
62</html>
63---
64Now for the code-behind file. First the NameSpace Imports. Notice that System.Web.Mail is included. Also notice that System.Web.UI.HtmlControls is also included. We must have that to work with the HtmlInput control that we use for browsing for an attachment. System.IO is needed because we must determine the full directory path for the attachment. Imports System
65Imports System.Web.Mail
66Imports System.Web
67Imports System.Web.UI
68Imports System.Web.UI.WebControls
69Imports System.Web.UI.HtmlControls
70Imports System.IO
71---
72Next is the declaration of the main class and declaration of the html and web controls we us in the program. Public Class SendEmail : Inherits Page
73
74Protected txtFrom As TextBox
75Protected txtTo As TextBox
76Protected txtSubject As TextBox
77Protected txtMessage As TextBox
78Protected ddlPriority As DropDownList
79Protected txtCC As TextBox
80Protected txtBCC As TextBox
81Protected txtAttachment As HtmlInputFile
82---
83Next is the button_click event where all the work is done. First the top of the code where we dimension the variables and objects we will need. We create both MailMessage and a SmtpMail objects. We also create a MailAttachment object. Next we get the contents of the HtmlInput control (postedFile) and dimension a variable to hold the path to the attachment. Notice that the "strPath =" line is inside a try-catch block. If you are not sending an attachment .Net chokes if you try to get the path to a null attachment object. Public Sub btnSend_Click(Sender As Object, e As EventArgs)
84Dim objMail As New MailMessage
85Dim objConn As SmtpMail
86Dim objAttach As MailAttachment
87Dim postedFile = txtAttachment.PostedFile
88Dim strPath As String = ""
89
90Try
91strPath = Path.GetFullPath(postedFile.FileName)
92Catch
93End Try
94---
95Now for the real work. For the most part properties of the MailMessage object (objMail) are being set to the contents of the textboxes and the message priority DropDownListBox. Notice again that the attachment object (objAttach) and the objMail.Attachements.Add are set within a Try-Catch block. Once again this is to protect against the possibility that no attachment is being sent. objMail.From = txtFrom.Text
96objMail.To = txtTo.Text
97objMail.Subject = txtSubject.Text
98objMail.Body = txtMessage.Text
99If ddlPriority.SelectedItem.Text = "Low" Then
100objMail.Priority = MailPriority.Low
101ElseIf ddlPriority.SelectedItem.Text = "Normal" Then
102objMail.Priority = MailPriority.Normal
103Else
104objMail.Priority = MailPriority.High
105End If
106objMail.Cc = txtCC.Text
107objMail.Bcc = TxtBCC.Text
108
109Try
110objAttach = New MailAttachment(strPath)
111objMail.Attachments.Add(objAttach)
112Catch
113End Try
114
115objConn.Send(objMail)
116End Sub
117
118End Class
119---
120Conclusion: You have just seen most of the properties and methods of the Mail and Smtp objects at work.</form>