例一:
input.htm
<html>
<head>
<title>精彩春风之ASP.NET发送 HTML格式邮件</title>
</head>
<body>
<form method="post" name="form1" action="emailhtml2.aspx">
邮件地址:<input type="text" name="EmailAddress" size="30" value="[email protected]"><br><br>
<input type="submit" value="发送" name="b1">
</form>
</body>
</html>
sendemail.asp
<%@ Import Namespace="System.Web.Util" %>
<script language="VB" runat=server>
Sub Page_load(Sender as Object, E as EventArgs)
Dim MyMessage as New MailMessage
MyMessage.To = request.form("EmailAddress")
MyMessage.From = "随风起舞"
MyMessage.Subject = "精彩春风之芊芊祝福!"
MyMessage.BodyFormat = MailFormat.Html
` 增加BodyFormat, 发送格式化的HTML,并删除这行和在message.的HTML代码,作为常规文本发送
MyMessage.Body = "<html><body><h1>您收到的邮件来自随风起舞!<ahref='http://intels.net'></h1></body></html>"
SmtpMail.Send(MyMessage)
End Sub
</script>
<html>
<head>
<title>精彩春风之ASP.NET发送 HTML格式邮件</title>
</head>
<body>
恭喜,你已发送了HTML 格式的邮件到:<br>
<h1><% response.write(request.form("EmailAddress")) %></h1>
</body>
</html>
例二:
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Util" %>
<%
MailMessage msgMail = new MailMessage();
msgMail.To = "[email protected] ";
msgMail.Cc = "[email protected]";
msgMail.From = "[email protected] ";
msgMail.Subject = "闪亮日子之ASP.NET发送HTML格式的邮件";
msgMail.BodyFormat = MailFormat.Html;
string strBody = "<html><body><b>闪亮日子</b>" +
" <font color=/"red/">春风精彩</font></body></html>";
msgMail.Body = strBody;
SmtpMail.Send(msgMail);
Response.Write("恭喜,发送成功!");
%>
例三:
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Util" %>
<%
string strTo = "[email protected]";
string strFrom = "[email protected]";
string strSubject = "闪亮日子之ASP.NET发送简单的邮件";
SmtpMail.Send(strFrom, strTo, strSubject,"春风精彩之文本文件");
Response.Write("恭喜,发送成功!");
%>
例四:
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Util" %>
<%
MailMessage msgMail = new MailMessage();
msgMail.To = "[email protected] ";
msgMail.From = "[email protected] ";
msgMail.Subject = "闪亮日子之ASP.NET发送带附件的邮件";
msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "闪亮日子之春风精彩!";
msgMail.Attachments.Add(new MailAttachment("c://shanliangrizi//jingcaitxt"));
SmtpMail.Send(msgMail);
Response.Write("恭喜,发送成功!");
%>
例五:
我们来使用.Net FrameWork SDK中的一个关于电子邮件发送的Class Library(类库)System.Web.Mail完成邮件发送。Class library提供了邮件发送的所有对象、属性和方法。其中最重要的对象是MailMessage对象和SmtpMail对象。MailMessage对象主要是包装电子邮件的收件人地址、发信人地址等;SmtpMail对象主要是把已经定义好的MailMessage对象发送出去。我们先在系统上建立一个指向此本.aspx文件的虚拟目录,就可以在浏览器里发送了。
<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">
Sub SendButton_Click(sender as Object, e as EventArgs)
' 创立一个 MailMessage 对象
Dim MailObj as New MailMessage()
if EmailFrom.text<>""then MailObj.From = EmailFrom.text
' 设定邮件的发送地址.下面是MailMessage 对象属性设定及程序常规判断
if EmailTo.text<>""then MailObj.To = EmailTo.text
' 设定邮件的目的地址如果发送给多个人,用逗号分开每个邮件地址。
if EmailCc.text<>""then MailObj.Cc = EmailCc.text
' 设定邮件的抄送地址.如果发送给多个人,可,写上多个邮件地址,但要用逗号把他们分开
if EmailBcc.text<>""then MailObj.Bcc = EmailBcc.text
' 设定邮件的密送地址
MailObj.BodyFormat = MailFormat.Text
' 设定邮件格式是文本格式,如果要设定成超文本,把MailFormat.Text改成 MailFormat.Html
MailObj.Priority = MailPriority.Normal
' 设定邮件优先级: High(高),Low(低), Normal(普通)
MailObj.Subject = EmailSubject.text
' 设定邮件主题
MailObj.Body = EmailBody.text
' 设定邮件内容
Dim strFileName as string
strFileName=Emailfile.PostedFile.FileName
if strFileName<>""then MailObj.Attachments.Add(new MailAttachment(strFileName))
' 给邮件增加一个附件
SmtpMail.SmtpServer = ""
' 指定使用缺省的SMTP服务器
SmtpMail.Send(MailObj)
' 开始发送邮件
panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub
</script>
<html>
<body>
<asp:panel id="panelSendEmail"runat="server">
<form Method="Post"EncType="Multipart/Form-Data"runat="server">
<h2>精彩春风之ASP.NET电子邮件发送</h2>
<b>请输入邮件发送地址:</b>
<asp:textbox id="EmailFrom"size="30"runat="server"/>
<p>
<b>请输入邮件目的地址:</b>
<asp:textbox id="EmailTo"size="30"runat="server"/>
<p>
<b>请输入邮件抄送地址:</b>
<asp:textbox id="EmailCc"size="30"runat="server"/>
<p>
<b>请输入邮件密送地址:</b>
<asp:textbox id="EmailBcc"size="30"runat="server" />
<p>
<b>请输入邮件主题:</b>
<asp:textbox id="EmailSubject"size="30" runat="server"/>
<p>
<b>请输入邮件主体:</b>
<asp:textbox id="EmailBody"TextMode="MultiLine"
Columns="40" Rows="10" runat="server"/>
<p>
<b>请加入附件名称:</b>
<input id="Emailfile" type="file" runat="server" size="40" />
<asp:button runat="server" id="SendButton" Text="发送"
OnClick="SendButton_Click" />
</form>
</asp:panel>
<asp:panel id="panelMailSent" runat="server" Visible="False" >
恭喜,您的邮件已经成功发送!
</asp:panel>
</body>
</html>