EmailHelper
注:个人邮箱发送时需要将邮箱密码设置为邮件授权码
邮件发送帮助类一:
public class EmailHelper { /// <summary> /// 发送邮件 /// </summary> /// <param name="To">收件地址</param> /// <param name="Name">收件人</param> /// <param name="Subject">标题</param> /// <param name="Content">内容</param> /// <returns></returns> public static bool SendEmail(string To, string Name, string Subject, string Content) { SmtpClient client = new SmtpClient(); //获取或设置用于验证发件人身份的凭据。 client.Credentials = new System.Net.NetworkCredential("邮箱地址", "密码"); client.Port =25; //端口号 //获取或设置用于 SMTP 事务的主机的名称或 IP 地址(这里是qq的,163邮箱的是smtp.163.com) client.Host = "smtp.qq.com"; try { client.Send(InitMailMessage(To, Name, Subject, Content)); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } /// <summary> /// 初始化信件相关信息 /// </summary> /// <param name="To">收件地址</param> /// <param name="name">收件人</param> /// <param name="Subject">标题</param> /// <param name="sendEmailContent">内容</param> /// <returns></returns> private static MailMessage InitMailMessage(string To, string name, string Subject, string sendEmailContent) { MailMessage mail = new MailMessage(); //发件人 mail.From = new MailAddress("邮箱地址", "设置个名称"); //收件人 if (name != "") { MailAddress mailAdd = new MailAddress(To, name); mail.To.Add(mailAdd); } else { mail.To.Add(To); } //主题 mail.Subject = Subject; //内容 mail.Body = sendEmailContent; //邮件主题和正文的编码格式 mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.BodyEncoding = System.Text.Encoding.UTF8; //邮件正文允许html编码 mail.IsBodyHtml = true; //优先级 mail.Priority = MailPriority.Normal; //密送——就是将信密秘抄送给收件人以外的人,所有收件人看不到密件抄送的地址 //mail.Bcc.Add(""); //抄送——就是将信抄送给收件人以外的人,所有的收件人可以在抄送地址处看到此信还抄送给谁 mail.CC.Add("抄送人地址"); mail.Attachments.Add(new Attachment(@"G:\2018\vue-devtools.zip")); //添加附件 return mail; } }
邮件发送帮助类二(感觉这个发的快一点):
public class EmailHelper { private MailMessage mMailMessage; //主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等) private SmtpClient mSmtpClient; //主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等) private int mSenderPort = 25; //发送邮件所用的端口号(htmp协议默认为25) private string mSenderServerHost = "smtp.exmail.qq.com"; //发件箱的邮件服务器地址(IP形式或字符串形式均可) private string mSenderPassword = "密码"; //发件箱的密码 private string mSenderUsername = "发件箱的用户名"; //发件箱的用户名 private bool mEnableSsl = false; //是否对邮件内容进行socket层加密传输 private bool mEnablePwdAuthentication = true; //是否对发件人邮箱进行密码验证 private string fromMail = "发件箱的地址"; //发件箱的地址(与用户名一致) /// <summary> /// 发送邮件 /// </summary> /// <param name="Receive">接收人邮箱</param> /// <param name="subject">标题</param> /// <param name="emailBody">邮件内容</param> /// <param name="cc">抄送人邮箱</param> public EmailHelper(List<string> Receive, string subject, string emailBody,List<string>cc) { mMailMessage = new MailMessage(); foreach (string item in Receive) { mMailMessage.To.Add(item); } mMailMessage.From = new MailAddress(fromMail); mMailMessage.Subject = subject; mMailMessage.Body = emailBody; mMailMessage.IsBodyHtml = true; mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; mMailMessage.Priority = MailPriority.Normal; foreach (string item in cc) { mMailMessage.To.Add(item); } } ///<summary> /// 添加附件 ///</summary> ///<param name="attachmentsPath">附件的路径集合,以分号分隔</param> public void AddAttachments(string attachmentsPath) { string[] path = attachmentsPath.Split(';'); //以什么符号分隔可以自定义 Attachment data; ContentDisposition disposition; for (int i = 0; i < path.Length; i++) { data = new Attachment(path[i], MediaTypeNames.Application.Octet); disposition = data.ContentDisposition; disposition.CreationDate = File.GetCreationTime(path[i]); disposition.ModificationDate = File.GetLastWriteTime(path[i]); disposition.ReadDate = File.GetLastAccessTime(path[i]); mMailMessage.Attachments.Add(data); } } ///<summary> /// 邮件的发送 ///</summary> public void Send() { if (mMailMessage != null) { mSmtpClient = new SmtpClient(); mSmtpClient.Host = this.mSenderServerHost; mSmtpClient.Port = this.mSenderPort; mSmtpClient.UseDefaultCredentials = false; mSmtpClient.EnableSsl = this.mEnableSsl; if (this.mEnablePwdAuthentication) { System.Net.NetworkCredential nc = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); //mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); //NTLM: Secure Password Authentication in Microsoft Outlook Express mSmtpClient.Credentials = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM"); } else { mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); } mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; mSmtpClient.Send(mMailMessage); } } }