using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
namespace Mail
{
public class MailHelper
{
/// <summary>
/// 邮件发送
/// </summary>
/// <param name="mailFrom">发件人信息</param>
/// <param name="listMailTo">收件人信息</param>
/// <param name="listMailCC">抄送人信息</param>
/// <param name="listMailBcc">密码送人信息</param>
/// <param name="strSubject">标题</param>
/// <param name="strBody">内容</param>
/// <param name="listAttachment">附件</param>
public void SendMail(MainFrom mailFrom, List<MailTo> listMailTo,List<MailCC> listMailCC,List<MailBcc> listMailBcc,string strSubject, string strBody, List<Attachments> listAttachment)
{
MailMessage mailMsg = new MailMessage();
//邮件的发件人
MailAddress from = new MailAddress(mailFrom.MailFromAddress, mailFrom.MailFromDisplayName, System.Text.Encoding.GetEncoding("GB2312"));
mailMsg.From = from;
//设置邮件的收件人
foreach (var item in listMailTo)
{
MailAddress to = new MailAddress(item.MailToAddress,item.MailToDisplayName);
mailMsg.To.Add(to);
}
//设置抄送人
foreach (var item in listMailCC)
{
MailAddress cc = new MailAddress(item.MailCCAddress,item.MailCCDisplayName);
mailMsg.CC.Add(cc);
}
foreach (var item in listMailBcc)
{
MailAddress bcc = new MailAddress(item.MailBccAddress,item.MailBccDisplayName);
mailMsg.CC.Add(bcc);
}
mailMsg.Subject = strSubject;
mailMsg.Body = strBody;
foreach (var item in listAttachment)
{
mailMsg.Attachments.Add(new Attachment(item.AttachmentPath));
}
mailMsg.IsBodyHtml = true;
mailMsg.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");
mailMsg.Priority = MailPriority.Normal;
mailMsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
//邮件发送服务器
SmtpClient client = new SmtpClient();
client.Host = "mail.lenovots.com";
//设置用于 SMTP 事务的端口,默认的是 25
//client.Port = 25;
client.UseDefaultCredentials = false;
//发件人邮箱登陆名和密码
client.Credentials = new System.Net.NetworkCredential("发件人邮箱地址", "发件人邮箱密码");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
//发送邮件
client.Send(mailMsg);
}
}
#region 发件人信息
public class MainFrom {
private string _mailFromAddress;
/// <summary>
/// 发件人邮箱地址
/// </summary>
public string MailFromAddress
{
get { return _mailFromAddress; }
set { _mailFromAddress = value; }
}
private string _mailFromDisplayName;
/// <summary>
/// 发件人显示名称
/// </summary>
public string MailFromDisplayName
{
get { return _mailFromDisplayName; }
set { _mailFromDisplayName = value; }
}
}
#endregion
#region 收件人信息
public class MailTo {
private string _mailToAddress;
/// <summary>
/// 收件人地址
/// </summary>
public string MailToAddress
{
get { return _mailToAddress; }
set { _mailToAddress = value; }
}
private string _mailToDisplayName;
/// <summary>
/// 收件人显示名称
/// </summary>
public string MailToDisplayName
{
get { return _mailToDisplayName; }
set { _mailToDisplayName = value; }
}
}
#endregion
#region 抄送人信息
public class MailCC {
private string _mailCC;
/// <summary>
/// 抄送人地址
/// </summary>
public string MailCCAddress
{
get { return _mailCC; }
set { _mailCC = value; }
}
private string _mailCCDisplayName;
/// <summary>
/// 抄送人显示名称
/// </summary>
public string MailCCDisplayName
{
get { return _mailCCDisplayName; }
set { _mailCCDisplayName = value; }
}
}
#endregion
#region 密送人信息
public class MailBcc {
private string _mailBccAddress;
/// <summary>
/// 密送人地址
/// </summary>
public string MailBccAddress
{
get { return _mailBccAddress; }
set { _mailBccAddress = value; }
}
private string _mailBccDisplayName;
/// <summary>
/// 密送人显示名称
/// </summary>
public string MailBccDisplayName
{
get { return _mailBccDisplayName; }
set { _mailBccDisplayName = value; }
}
}
#endregion
#region 附件信息
public class Attachments {
private string _attachmentPath;
/// <summary>
/// 附件路径
/// </summary>
public string AttachmentPath
{
get { return _attachmentPath; }
set { _attachmentPath = value; }
}
}
#endregion