asp.net.邮件单发和群发
注:如果此类和项目分层,请添加引用using System.Web;(引用)才能实现1和2的引用
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Web.Util;//////////1引用
using System.Web.Mail;//////////2引用
namespace Bll
{
public class sendmail //服务邮箱尽量不要换,需要资格用户才能发送邮件
{
protected static string smtp = "企业邮箱";
protected static string username = "用户名";
protected static string password = "密码";
public static bool sendMail(string addressee, string title, string content)
{
try
{
MailMessage msg = new MailMessage();
msg.From = username;
msg.To = addressee;
msg.Subject = title;
msg.Body = content;
msg.Priority = MailPriority.Normal; //设置优先集
msg.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = smtp;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
//cdoBasic 基本验证 gmail使用ssl验证
//msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "No");
//账号
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username);
//密码
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
SmtpMail.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
public static bool sendColonyMail(string[] addresseeList, string title, string content)
{
try
{
MailMessage msg = new MailMessage();
msg.From = username;
string straddressee = "";
foreach (string addressee in addresseeList)
{
straddressee += ","+addressee;
}
msg.To = straddressee.Substring(1, straddressee.Length);
msg.Subject = title;
msg.Body = content;
msg.Priority = MailPriority.Normal; //设置优先集
msg.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = smtp;
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1);
//cdoBasic 基本验证 gmail使用ssl验证
// msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "No");
//账号
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", username);
//密码
msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
SmtpMail.Send(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}