Asp.Net给多用户发邮件完整代码

发信类sendMail.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
/**/ ///<summary>
///sendMail的摘要说明
///</summary>

public class sendMail
... {
publicsendMail()
...{
//
//TODO:在此处添加构造函数逻辑
//
}

/**////<summary>
///发送邮件
///</summary>
///<paramname="from">发送人邮件地址</param>
///<paramname="to">接收人邮件地址</param>
///<paramname="subject">邮件主题</param>
///<paramname="isBodyHtml">是否是Html</param>
///<paramname="body">邮件体</param>
///<paramname="smtpHost">SMTP服务器地址,例如:smtp.163.com</param>
///<paramname="userName">用户名</param>
///<paramname="password">密码</param>
///<returns>是否成功</returns>

publicstaticboolSend(stringfrom,stringto,stringsubject,boolisBodyHtml,stringbody,stringsmtpHost,stringuserName,stringpassword)
...{
string[]ts=to.Split(',');
boolisSuccess=true;
foreach(stringtints)
...{
try
...{
MailMessagemm
=newMailMessage();
mm.From
=newMailAddress(from);

mm.To.Add(
newMailAddress(t.Trim()));

mm.Subject
=subject;
mm.IsBodyHtml
=isBodyHtml;
mm.Body
=body;

SmtpClientsc
=newSmtpClient();
sc.Host
=smtpHost;

sc.UseDefaultCredentials
=true;//winform中不受影响,asp.net中,false表示不发送身份严正信息
//smtpClient.EnableSsl=true;//如果服务器不支持ssl则报,服务器不支持安全连接错误
sc.Credentials=newSystem.Net.NetworkCredential(userName,password);
sc.DeliveryMethod
=SmtpDeliveryMethod.Network;

sc.Send(mm);
}

catch
...{
isSuccess
=false;
}

}

returnisSuccess;
}

}

调用发信函数

if (sendMail.Send( " [email protected] " , " [email protected] " , " subject " , true , " body " , " smtp.163.com " , " yangcai " , " yangcai " ))
... {
Response.Write(
"ok");
}

else
... {
Response.Write(
"no");
}

本例的类下载地址: http://www.cnblogs.com/Files/yangcai/sendMail.rar

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1626576

你可能感兴趣的:(asp.net)