国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Send Email and Attachments with ASP.Net...

2024-07-10 13:12:14
字體:
來源:轉載
供稿:網友
,歡迎訪問網頁設計愛好者web開發。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.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 措勤县| 凤城市| 临泉县| 谷城县| 行唐县| 南城县| 龙口市| 兴隆县| 乐安县| 宾川县| 红桥区| 卢湾区| 泾川县| 普定县| 武冈市| 且末县| 晋城| 磴口县| 北安市| 中阳县| 裕民县| 朝阳县| 偏关县| 阿坝县| 金溪县| 尼勒克县| 武功县| 濉溪县| 奎屯市| 韶山市| 会宁县| 疏勒县| 石城县| 白玉县| 深泽县| 谢通门县| 新闻| 顺义区| 玉溪市| 伊川县| 辽阳市|