是4.0的,说以添加包是 代码: #region 邮件类 /// summary /// 邮件发送类 /// /summary public class EmailSend { /// summary /// 发件人 /// /summary public Addresser addresser { get ; set ; } /// summary /// 收件人 ///
是4.0的,说以添加包是
代码:
#region 邮件类 /// <summary> /// 邮件发送类 /// </summary> public class EmailSend { /// <summary> /// 发件人 /// </summary> public Addresser addresser { get; set; } /// <summary> /// 收件人 /// </summary> public List<string> recipients { get; set; } /// <summary> /// 标题 /// </summary> public string titlite { get; set; } /// <summary> /// 内容 /// </summary> public string context { get; set; } /// <summary> /// 附件地址 /// </summary> public List<string> accessoryPath { get; set; } } /// <summary> /// 发件人 /// </summary> public class Addresser { /// <summary> /// 账号名称 /// </summary> public string name { get; set; } /// <summary> /// 账号 /// </summary> public string caccount { get; set; } /// <summary> /// 秘钥 /// </summary> public string password { get; set; } } #endregion
/// <summary> /// 发送邮件 /// </summary> /// <param name="model"></param> private void Send163Email(EmailSend model) { MailMessage mailMsg = new MailMessage(); //发件人 mailMsg.From = new MailAddress(model.addresser.caccount); //收件人 foreach (var it in model.recipients) { mailMsg.To.Add(new MailAddress(it)); } //标题 mailMsg.Subject = model.titlite; //正文 mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(model.context, Encoding.UTF8, MediaTypeNames.Text.Html)); //附件 if (model.accessoryPath != null) foreach (string path in model.accessoryPath) { mailMsg.Attachments.Add(new Attachment(path)); } SmtpClient smtpClient = new SmtpClient("smtp.126.com", 25); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(model.addresser.name, model.addresser.password); smtpClient.Credentials = credentials; smtpClient.Send(mailMsg); smtpClient.Dispose(); }
demo的地址:https://gitee.com/cainiaoA/sendEmail