public class Mail
{
private string mailkey;
private string mailSecret;
public string mailFrom; public Mail()
{
this.mailkey = BaseHelper.GetValue("MailKey");
this.mailSecret = BaseHelper.GetValue("MailSecret");
this.mailFrom = BaseHelper.GetValue("MailFrom");
} public Mail(string key,string secret,string username)
{
this.mailkey = key;
this.mailSecret = secret;
this.mailFrom = username;
} /// <summary>
/// 群发邮件
/// </summary>
/// <param name="title">标题</param>
/// <param name="content">内容</param>
/// <param name="toUsers">接收方</param>
/// <returns></returns>
public ResultDTO Send(string title,string content,string[] toUsers)
{
try
{
MailMessage mail = new MailMessage();
mail.Subject = title;
mail.From = new MailAddress(this.mailkey,this.mailFrom);
foreach (var user in toUsers)
{
mail.To.Add(new MailAddress(user));
}
mail.Body = content; mail.BodyEncoding = Encoding.UTF8;
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.163.com";
client.Port = 25;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(this.mailkey,this.mailSecret);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(mail);
return WebApi.Success("邮件发送成功!");
}
catch(Exception e)
{
return WebApi.Error(e.Message);
}
} /// <summary>
/// 单发邮件
/// </summary>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="toUser"></param>
/// <returns></returns>
public ResultDTO Send(string title, string content, string toUser)
{
return Send(title, content, new[] {toUser});
}
}