正文

在asp.NET 中使用SMTP发送邮件的实现代码2012-08-12 11:25:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/edwardguo/53712.html

分享到:

核心代码:
复制代码 代码如下:
public class Mail
{
#region 邮件参数
static public string accountName = System.Configuration.ConfigurationManager.AppSettings["SmtpAccountName"];
static public string password = System.Configuration.ConfigurationManager.AppSettings["SmtpAccountPW"];
static public string smtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
static public int smtpPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
#endregion

/// <summary>
/// 邮件发送方法一
/// </summary>
/// <param name="sendTo"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
static public void SendMail(string sendTo, string subject, string body)
{
//.net smtp
System.Web.Mail.MailMessage mailmsg = new System.Web.Mail.MailMessage();
mailmsg.To = sendTo;
//mailmsg.Cc = cc;
mailmsg.Subject = subject;
mailmsg.Body = body;
mailmsg.BodyFormat = MailFormat.Html;


//sender here
mailmsg.From = Mail.accountName;
// certify needed
mailmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");//1 is to certify
//the user id
mailmsg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendusername",
Mail.accountName);
//the password
mailmsg.Fields.Add(
"http://schemas.microsoft.com/cdo/configuration/sendpassword",
Mail.password);

System.Web.Mail.SmtpMail.SmtpServer = Mail.smtpServer;

System.Web.Mail.SmtpMail.Send(mailmsg);

}
/// <summary>
/// 邮件发送方法二
/// </summary>
/// <param name="sendTo"></param>
/// <param name="subject"></param>
/// <param name="body"></param>
static public void SendMail2(string sendTo, string subject, string body)
{
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(accountName, sendTo, subject, body);
msg.From = new System.Net.Mail.MailAddress(accountName, "Mail");
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtpServer);
msg.IsBodyHtml = true;
client.Credentials = new System.Net.NetworkCredential(accountName, password);
client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

client.Send(msg);
}
}


详细出处参考:http://www.jb51.net/article/27183.htm

阅读(1084) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册