您的位置:首页 > 编程语言 > ASP

asp.net 发送HTML格式邮件及发送Html模版邮件

2013-01-30 14:50 453 查看
今天闲来无事,动手用asp.net做了个发送HTML格式邮件及发送Html模版邮件的小程序

用的是网易126邮箱的smtp服务器,在126邮箱中可以设置,不过一般已经默认开通了

为了编码的简洁与程序的灵活性,所以我通过配置Web.config文件的方式来进行应用程序的控制,在这个配置节属于<system.net>节下增加如下配置节:

<!--配置发送邮件-->
<system.net>
<mailSettings>
<smtp from="longbiao20082008@126.com" deliveryMethod="Network">
<network host="smtp.126.com" port="25" password="邮箱密码" userName="邮箱用户名"/>
</smtp>
</mailSettings>

发送邮件事件

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//using System.Web.Mail;
using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mMessage = new MailMessage();
string attachment = FileUpload1.PostedFile.FileName; //获取附件路径
Attachment ma = new Attachment(attachment);
mMessage.Attachments.Add(ma);  //添加附件
mMessage.To.Add(TextBox1.Text.Trim());
mMessage.Subject = TextBox2.Text.Trim();
//将HTML网页作为内容发送
mMessage.IsBodyHtml = true;
//mMessage.Body = "<h7><font color='red'>" + TextArea1.Value.Trim() + "</font></h7>";
mMessage.BodyEncoding = System.Text.Encoding.UTF8;
mMessage.Body = RePlaceHtmlContent("LongBiao",DateTime.Now.ToShortDateString());
// SmtpMail.Send(mMessage);
System.Net.Mail.SmtpClient sc = new SmtpClient();
sc.Send(mMessage);
Response.Write("<script>alert('发送成功!');</script>");

}
//读取HTML网页,并动态替换需要更改的内容
private string RePlaceHtmlContent(string name,string time)
{
string path = HttpContext.Current.Server.MapPath("HTMLPage.htm");
System.IO.StreamReader sr = new System.IO.StreamReader(path);
string str = sr.ReadToEnd();
str=str.Replace("$name",name);
str=str.Replace("$time", time);
return str;
}
}


HTML模版

<body>

<p style="font-family: 黑体; font-size: x-large; color: #FF0000">
测试发送HTML格式邮件</p>
<p style="font-family: 黑体; font-size: large; color: #008000">
发送人:$name  发送时间:$time</p>
<p style="font-family: 黑体; font-size: x-large; color: #FF0000">
<img alt="HTML格式邮件" src="http://pic17.nipic.com/20111125/3771533_145240633000_2.jpg" style="height: 373px; width: 727px" /></p>

</body>


最后测试发送邮件成功

如果不通过配置文件来进行配置smtp服务器的话,可如下操作

string mailServerName = "smtp.126.com";  //发送邮件的SMTP服务器
string mailFrom = "longbiao20082008@126.com";   //发件人邮箱(用126的邮件服务器,就必须用126邮箱的用户名)
string mailTo = "123456789@qq.com";   //收件人邮箱
string subject = "用代码方式发送邮件";//邮件主题
string body = "这里是邮件正文了";  //邮件正文

using (MailMessage message = new MailMessage(mailFrom, mailTo, subject, body))
{
//SmtpClient是发送邮件的主体,这个构造函数是告知SmtpClient发送邮件时使用哪个SMTP服务器
SmtpClient mailClient = new SmtpClient(mailServerName);
//将认证实例赋予mailClient,也就是访问SMTP服务器的用户名和密码
mailClient.Credentials = new NetworkCredential("sender0624", "111111");
//最终的发送方法
mailClient.Send(message);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: