郵件發送類 來源網上 稍作調整。。。出處忘了

/** * 命名空間: EmailSend * 類 名: EmailSend * * 作者 變更內容 變更日期 * ───────────────────────────────── * XXX 初版 2018-10-31 09:55:42 */ using System; using System.Collections.Generic; using System.Linq; using System.Net.Mail; using System.Text; namespace EmailSend { public class EmailSend { /// <summary> /// 發送者 /// </summary> public string mailFrom { get; set; } /// <summary> /// 收件人 /// </summary> public string[] mailToArray { get; set; } public enumSMTPType mailSMTP { get; set; } /// <summary> /// 抄送 /// </summary> public string[] mailCcArray { get; set; } /// <summary> /// 標題 /// </summary> public string mailSubject { get; set; } /// <summary> /// 正文 /// </summary> public string mailBody { get; set; } /// <summary> /// 發件人密碼 /// </summary> public string mailPwd { get; set; } /// <summary> /// SMTP郵件服務器 /// </summary> public string host { get; set; } /// <summary> /// 正文是否是html格式 /// </summary> public bool isbodyHtml { get; set; } /// <summary> /// 附件 /// </summary> public string[] attachmentsPath { get; set; } #region 發送郵件 /// <summary> /// 發送郵件 /// </summary> /// <returns></returns> public bool Send() { //使用指定的郵件地址初始化MailAddress實例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage實例 MailMessage myMail = new MailMessage(); //向收件人地址集合添加郵件地址 if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //向抄送收件人地址集合添加郵件地址 if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //發件人地址 myMail.From = maddr; //電子郵件的標題 myMail.Subject = mailSubject; //電子郵件的主題內容使用的編碼 myMail.SubjectEncoding = Encoding.UTF8; //電子郵件正文 myMail.Body = mailBody; //電子郵件正文的編碼 myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //在有附件的情況下添加附件 try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("在添加附件時有錯誤:" + err); } SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; //指定發件人的郵件地址和密碼以驗證發件人身份 smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); //設置SMTP郵件服務器 smtp.Host = host; try { //將郵件發送到SMTP郵件服務器 smtp.Send(myMail); return true; } catch (System.Net.Mail.SmtpException ex) { return false; } } #endregion #region 發送郵件顯示異常 /// <summary> /// 發送郵件顯示異常 /// </summary> /// <returns></returns> public bool Send(out string errs) { //使用指定的郵件地址初始化MailAddress實例 MailAddress maddr = new MailAddress(mailFrom); //初始化MailMessage實例 MailMessage myMail = new MailMessage(); //向收件人地址集合添加郵件地址 if (mailToArray != null) { for (int i = 0; i < mailToArray.Length; i++) { myMail.To.Add(mailToArray[i].ToString()); } } //向抄送收件人地址集合添加郵件地址 if (mailCcArray != null) { for (int i = 0; i < mailCcArray.Length; i++) { myMail.CC.Add(mailCcArray[i].ToString()); } } //發件人地址 myMail.From = maddr; //電子郵件的標題 myMail.Subject = mailSubject; //電子郵件的主題內容使用的編碼 myMail.SubjectEncoding = Encoding.UTF8; //電子郵件正文 myMail.Body = mailBody; //電子郵件正文的編碼 myMail.BodyEncoding = Encoding.Default; myMail.Priority = MailPriority.High; myMail.IsBodyHtml = isbodyHtml; //在有附件的情況下添加附件 try { if (attachmentsPath != null && attachmentsPath.Length > 0) { Attachment attachFile = null; foreach (string path in attachmentsPath) { attachFile = new Attachment(path); myMail.Attachments.Add(attachFile); } } } catch (Exception err) { throw new Exception("在添加附件時有錯誤:" + err); } SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; //指定發件人的郵件地址和密碼以驗證發件人身份 smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd); //設置SMTP郵件服務器 smtp.Host = host; try { //將郵件發送到SMTP郵件服務器 smtp.Send(myMail); errs = ""; return true; } catch (System.Net.Mail.SmtpException ex) { errs = ex.Message; return false; } } #endregion } }
發送斷代碼

private void button2_Click(object sender, EventArgs e) { Email email = new Email(); email.mailFrom = "aaaa@126.com"; email.mailPwd = "密碼"; email.mailSubject = "郵件主題31"; email.mailBody = "這是一封測試郵件"; email.isbodyHtml = false; //是否是HTML email.host = "smtp.126.com";//如果是QQ郵箱則:smtp:qq.com,依次類推 // email.host = "smtp.qq.com"; email.mailToArray = new string[] { "333@qq.com" };//接收者郵件集合 //email.mailCcArray = new string[] { "******@qq.com" };//抄送者郵件集合 if (email.Send()) { MessageBox.Show("ok"); } else MessageBox.Show("NoOK"); }
發送的郵件要開通smtp功能。這個網站進入郵箱-->設置-->pop/smtp可以找到