分享
 
 
 

Send Email and Attachments with ASP.Net...

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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 <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>

Now 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

Imports System.Web.Mail

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Web.UI.HtmlControls

Imports System.IO

Next 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

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

Next 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)

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

Now 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

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

Conclusion: You have just seen most of the properties and methods of the Mail and Smtp objects at work.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有