using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Helpers
{
///
/// 邮件发送助手,弃用,该类已移至PublicCode类库中
///
public class MailHelper
{
public string host { get; set; }
public string senderMail { get; set; }
public string senderKey { get; set; }
public string senderName { get; set; }
public MailHelper() { }
public MailHelper(string host) {
this.host = host;
}
public MailHelper(string host,string senderMail,string senderKey) {
this.host = host;
this.senderMail = senderMail;
this.senderKey = senderKey;
}
public bool Send(string title,string htmlContent,List toMails, MailPriority mailPriority = MailPriority.Normal) {
if (!ParamCheck()) {
throw new Exception("未设置有效的连接参数");
}
if (toMails.Count == 0) {
throw new Exception("接收者不能为空");
}
if (string.IsNullOrWhiteSpace(title)) {
title = "无主题";
}
SmtpClient mail = new SmtpClient(this.host);
mail.Credentials = new NetworkCredential(senderMail, senderKey);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = title;
msg.SubjectEncoding = Encoding.UTF8;
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = mailPriority;
#region 邮件内容设置
StringBuilder body = new StringBuilder();
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#endregion
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
msg.From = fromAddress;
foreach (var a in toMails) {
MailAddress toAddress = new MailAddress(a);
msg.To.Add(toAddress);
}
mail.Send(msg);
mail.Dispose();
return true;
}
public bool Send(string title, string htmlContent, List toMails,Dictionary files, MailPriority mailPriority = MailPriority.Normal) {
if (!ParamCheck())
{
throw new Exception("未设置有效的连接参数");
}
if (toMails.Count == 0)
{
throw new Exception("接收者不能为空");
}
if (string.IsNullOrWhiteSpace(title))
{
title = "无主题";
}
SmtpClient mail = new SmtpClient(this.host);
mail.Credentials = new NetworkCredential(senderMail, senderKey);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = title;
msg.SubjectEncoding = Encoding.UTF8;
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = mailPriority;
#region 附件添加
foreach (var a in files) {
if (File.Exists(a.Value)){
var t = new Attachment(a.Value);
t.ContentId = a.Key;
msg.Attachments.Add(t);
}
}
#endregion
#region 邮件内容设置
StringBuilder body = new StringBuilder();
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#endregion
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
msg.From = fromAddress;
foreach (var a in toMails)
{
MailAddress toAddress = new MailAddress(a);
msg.To.Add(toAddress);
}
mail.Send(msg);
mail.Dispose();
return true;
}
public bool Send(string title, string htmlContent, List toMails, List CC,bool IsBCC=false, MailPriority mailPriority = MailPriority.Normal) {
if (!ParamCheck())
{
throw new Exception("未设置有效的连接参数");
}
if (toMails.Count == 0)
{
throw new Exception("接收者不能为空");
}
if (string.IsNullOrWhiteSpace(title))
{
title = "无主题";
}
SmtpClient mail = new SmtpClient(this.host);
mail.Credentials = new NetworkCredential(senderMail, senderKey);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = title;
msg.SubjectEncoding = Encoding.UTF8;
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = mailPriority;
#region 邮件内容设置
StringBuilder body = new StringBuilder();
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#endregion
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
msg.From = fromAddress;
foreach (var a in toMails)
{
MailAddress toAddress = new MailAddress(a);
msg.To.Add(toAddress);
}
if (IsBCC) {
foreach (var a in CC) {
msg.Bcc.Add(a);
}
} else {
foreach (var a in CC) {
msg.CC.Add(a);
}
}
mail.Send(msg);
mail.Dispose();
return true;
}
public bool Send(string title, string htmlContent, List toMails, Dictionary files, MailAddressCollection CC,bool IsBCC = false, MailPriority mailPriority = MailPriority.Normal) {
if (!ParamCheck())
{
throw new Exception("未设置有效的连接参数");
}
if (toMails.Count == 0)
{
throw new Exception("接收者不能为空");
}
if (string.IsNullOrWhiteSpace(title))
{
title = "无主题";
}
SmtpClient mail = new SmtpClient(this.host);
mail.Credentials = new NetworkCredential(senderMail, senderKey);
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Subject = title;
msg.SubjectEncoding = Encoding.UTF8;
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = mailPriority;
#region 附件添加
foreach (var a in files)
{
if (File.Exists(a.Value))
{
var t = new Attachment(a.Value);
t.ContentId = a.Key;
msg.Attachments.Add(t);
}
}
#endregion
#region 邮件内容设置
StringBuilder body = new StringBuilder();
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine("");
body.AppendLine(htmlContent);
body.AppendLine("");
body.AppendLine("");
#endregion
MailAddress fromAddress = new MailAddress(senderMail, senderName, Encoding.UTF8);
msg.From = fromAddress;
foreach (var a in toMails)
{
MailAddress toAddress = new MailAddress(a);
msg.To.Add(toAddress);
}
if (IsBCC)
{
foreach (var a in CC)
{
msg.Bcc.Add(a);
}
}
else
{
foreach (var a in CC)
{
msg.CC.Add(a);
}
}
mail.Send(msg);
mail.Dispose();
return true;
}
private bool ParamCheck() {
if (string.IsNullOrWhiteSpace(host)|| string.IsNullOrWhiteSpace(senderMail)
|| string.IsNullOrWhiteSpace(senderKey) || string.IsNullOrWhiteSpace(senderName)) {
return false;
}
return true;
}
}
}