发邮件代码
Code
/// <summary>
/// 外发邮件支持群发
/// </summary>
/// <param name="MessageFrom">发件人</param>
/// <param name="MessageTo">群发多用户使用"|"格开地址 没有检测邮件地址格式</param>
/// <param name="MessageSubject">邮件主题</param>
/// <param name="MessageBody">邮件内容</param>
/// <returns>是否发送成功</returns>
public bool SendMail(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
SmtpClient sc = new SmtpClient();
sc.Host = ConfigurationManager.AppSettings["smtp"].ToString(); //指定发送邮件的服务器地址或IP
sc.Port = 25; //指定发送邮件端口
sc.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpuser"].ToString(), ConfigurationManager.AppSettings["smtppassword"].ToString()); //指定登录服务器的用户名和密码
MailMessage message = new MailMessage();
message.From = MessageFrom;
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true; //是否为html格式
message.Priority = MailPriority.High; //发送邮件的优先等级
string[] mtuser = MessageTo.Split('|');
foreach (string m in mtuser)
{
if (m != "" && m != null)
{
message.To.Clear();
message.To.Add(m);
try
{
sc.Send(message); //发送邮件
}
catch
{
return false;
}
}
}
return true;
}
/// <summary>
/// 外发邮件支持群发
/// </summary>
/// <param name="MessageFrom">发件人</param>
/// <param name="MessageTo">群发多用户使用"|"格开地址 没有检测邮件地址格式</param>
/// <param name="MessageSubject">邮件主题</param>
/// <param name="MessageBody">邮件内容</param>
/// <returns>是否发送成功</returns>
public bool SendMail(MailAddress MessageFrom, string MessageTo, string MessageSubject, string MessageBody)
{
SmtpClient sc = new SmtpClient();
sc.Host = ConfigurationManager.AppSettings["smtp"].ToString(); //指定发送邮件的服务器地址或IP
sc.Port = 25; //指定发送邮件端口
sc.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpuser"].ToString(), ConfigurationManager.AppSettings["smtppassword"].ToString()); //指定登录服务器的用户名和密码
MailMessage message = new MailMessage();
message.From = MessageFrom;
message.Subject = MessageSubject;
message.Body = MessageBody;
message.IsBodyHtml = true; //是否为html格式
message.Priority = MailPriority.High; //发送邮件的优先等级
string[] mtuser = MessageTo.Split('|');
foreach (string m in mtuser)
{
if (m != "" && m != null)
{
message.To.Clear();
message.To.Add(m);
try
{
sc.Send(message); //发送邮件
}
catch
{
return false;
}
}
}
return true;
}