asp.net发邮件的例程(language:”c#”; 带email地址验证)
——————————————————————————–
<%@ page language = “c#” debug = “true” explicit=”true” %>
<%@ import namespace = “system.web.util” %>
<%@ import namespace = “system.web.mail” %>
<script language = “c#” runat = “server”>
public void sendmail (object obj, eventargs e) {
labelsendmailresult.text = “”;
if (page.isvalid) {
mailmessage mailobj = new mailmessage();
// 设置email的from和to的地址
mailobj.from = inputmailfrom.value;
mailobj.to = inputmailto.value;
mailobj.subject = inputmailsubject.value;
mailobj.body = textboxmailbody.text;
// 可选: 使用html格式的email
mailobj.bodyformat = mailformat.html;
// 可选: 对邮件进行加密
// mailobj.bodyencoding = mailformat.base64;
// 可选: 设置邮件的优先级别为高
mailobj.priority = mailpriority.high;
// 可选: 附件
if (inputmailattachment.postedfile.contentlength > 0) {
// 注意这里我们创建了一个mailattachment对象来附加一个文件到email。
mailobj.attachments.add(new mailattachment(inputmailattachment.postedfile.filename));
}
// 使用smtpmail对象来发送邮件。
smtpmail.send(mailobj);
labelsendmailresult.text = “邮件发送成功 from: ” + inputmailfrom.value + “; to: ” + inputmailto.value;
if (inputmailattachment.postedfile.contentlength > 0) {
labelsendmailresult.text += “<br>该邮件包含附件: ” + inputmailattachment.postedfile.filename + “, 附件大小为: ” + (inputmailattachment.postedfile.contentlength / 1024).tostring() + ” k byte(s)”;
}
}
}
</script>
<html>
<head>
<title>
发送邮件 asp.net</title>
</head>
<body>
<div align=”center”>
<table width=”100%” border=”0″ cellpadding=”0″ cellspacing=”0″ style=”border-collapse: collapse” bordercolor=”#eeeeee” id=”autonumber1″>
<form id=”formmail” method=”post” action=”” enctype=”multipart/form-data” runat=”server”>
<tr>
<td width=”20%” height=”24″>收件人地址: </td>
<td width=”80%” height=”24″>
<input type=”text” id=”inputmailto” name=”inputmailto” runat=”server” size=”48″>
<asp:requiredfieldvalidator
id=”requiredfieldvalidatorinputmailto”
controltovalidate=”inputmailto”
display=”static”
enableclientscript=”false”
errormessage=”收件人不能为空”
runat=”server”/>
<asp:regularexpressionvalidator id=”regularexpressionvalidatorinputmailto”
controltovalidate=”inputmailto”
validationexpression=”^[\w\.-]+@[\w\.-]+\.[a-za-z]+$”
display=”static”
enableclientscript=”false”
errormessage=”收件人邮件地址错误”
runat=”server”/>
</td>
</tr>
<tr>
<td width=”20%” height=”24″>发件人地址: </td>
<td width=”80%” height=”24″>
<input type=”text” id=”inputmailfrom” name=”inputmailfrom” runat=”server” size=”48″>
<asp:requiredfieldvalidator
id=”requiredfieldvalidatorinputmailfrom”
controltovalidate=”inputmailfrom”
display=”static”
enableclientscript=”false”
errormessage=”发件人不能为空”
runat=”server”/>
<asp:regularexpressionvalidator id=”regularexpressionvalidatorinputmailfrom”
controltovalidate=”inputmailfrom”
validationexpression=”^[\w\.-]+@[\w\.-]+\.[a-za-z]+$”
display=”static”
enableclientscript=”false”
errormessage=”发件人邮件地址错误”
runat=”server”/>
</td>
</tr>
<tr>
<td width=”20%” height=”24″>邮件主题: </td>
<td width=”80%” height=”24″>
<input type=”text” id=”inputmailsubject” name=”inputmailsubject” runat=”server” size=”48″>
<asp:requiredfieldvalidator
id=”requiredfieldvalidatorinputmailsubject”
controltovalidate=”inputmailsubject”
display=”static”
enableclientscript=”false”
errormessage=”邮件主题不能为空”
runat=”server”/>
</td>
</tr>
<tr>
<td width=”20%” height=”24″>邮件内容:
</td>
<td width=”80%” height=”24″>
<asp:textbox id=”textboxmailbody” name=”textboxmailbody” runat=”server” rows=”6″ cols=”48″ textmode=”multiline” />
<asp:requiredfieldvalidator
id=”requiredfieldvalidatortextboxmailbody”
controltovalidate=”textboxmailbody”
display=”static”
enableclientscript=”false”
errormessage=”邮件内容不能为空”
runat=”server”/>
</td>
</tr>
<tr>
<td width=”20%” height=”24″>邮件附件:
</td>
<td width=”80%” height=”24″>
<input type=”file” id=”inputmailattachment” name=”inputmailattachment” runat=”server” size=”48″>
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<input type=”button” value=”send mail” onserverclick=”sendmail” id=”buttonsendmail” name=”buttonsendmail” runat=”server”>
</td>
</tr>
<tr>
<td colspan=”2″ align=”center” height=”24″>
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<asp:label id=”labelsendmailresult” runat=”server” text=”” font-bold=”true” forecolor=”#ff0000″/>
</td>
</tr>
</form>
</table>
</div>
</body>
</html>