首页 > 编程 > .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.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表