using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;using System.Net.Mime;using System.IO;using System.Timers;using System.Xml;using System.Web;using CommonClass.StringHander;namespace CommonClass.Mail{ public class MyEmail { private MailMessage mMailMessage; //主要處理髮送郵件的內容(如:收發人地址、標題、主體、圖片等等) private SmtpClient mSmtpClient; //主要處理用smtp方式發送此郵件的配置資訊(如:郵件伺服器、傳送埠號、驗證方式等等) private int mSenderPort; //發送郵件所用的連接埠號碼(htmp協議預設為25) private string mSenderServerHost; //寄件匣的郵件伺服器地址(IP形式或字串形式均可) private string mSenderPassword; //寄件匣的密碼 private string mSenderUsername; //寄件匣的使用者名稱(即@符號前面的字串,例如:hello@163.com,使用者名稱為:hello) private bool mEnableSsl; //是否對郵件內容進行socket層加密傳輸 private bool mEnablePwdAuthentication; //是否對寄件者郵箱進行密碼驗證 /// <summary> /// 建構函式 /// </summary> /// <param name="server">寄件匣的郵件伺服器地址</param> /// <param name="toMail">收件者地址(可以是多個收件者,程式中是以“;"進行區分的)</param> /// <param name="fromMail">寄件者地址</param> /// <param name="subject">郵件標題</param> /// <param name="emailBody">郵件內容(可以以html格式進行設計)</param> /// <param name="displayName">發送後顯示發送人郵件地址的別名</param> /// <param name="username">寄件匣的使用者名稱(即@符號前面的字串,例如:hello@163.com,使用者名稱為:hello)</param> /// <param name="password">寄件者郵箱密碼</param> /// <param name="port">發送郵件所用的連接埠號碼(htmp協議預設為25)</param> /// <param name="sslEnable">true表示對郵件內容進行socket層加密傳輸,false表示不加密</param> /// <param name="pwdCheckEnable">true表示對寄件者郵箱進行密碼驗證,false表示不對寄件者郵箱進行密碼驗證</param> public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody,string displayName, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) { try { mMailMessage = new MailMessage(); mMailMessage.To.Add(toMail); mMailMessage.From = new MailAddress(fromMail,displayName); mMailMessage.Subject = subject; mMailMessage.Body = emailBody; mMailMessage.IsBodyHtml = true; mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; mMailMessage.Priority = MailPriority.Normal; this.mSenderServerHost = server; this.mSenderUsername = username; this.mSenderPassword = password; this.mSenderPort = Convert.ToInt32(port); this.mEnableSsl = sslEnable; this.mEnablePwdAuthentication = pwdCheckEnable; } catch (Exception ex) { //Console.WriteLine(ex.ToString()); } } /// <summary> /// 添加附件 /// </summary> /// <param name="attachmentsPath">附件的路徑集合,以分號分隔</param> public void AddAttachments(string attachmentsPath) { try { 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); } } catch (Exception ex) { //Console.WriteLine(ex.ToString()); } } /// <summary> /// 郵件的發送 /// </summary> /// <returns>true:發送成功</returns> public bool Send() { if (!GetMailSetting.IsEnable) { return false; } bool flag = false; try { 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 = nc.GetCredential(mSmtpClient.Host, mSmtpClient.Port, "NTLM"); } else { mSmtpClient.Credentials = new System.Net.NetworkCredential(this.mSenderUsername, this.mSenderPassword); } mSmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; mSmtpClient.Send(mMailMessage); flag = true; } } catch (Exception ex) { //Console.WriteLine(ex.ToString()); } return flag; } /// <summary> /// 擷取郵件配置 /// </summary> public static MailSetting GetMailSetting { get { XmlDocument doc = new XmlDocument(); doc.Load(HttpContext.Current.Request.MapPath("~/Config/Mail.config")); MailSetting smtpSetting = new MailSetting(); smtpSetting.Server = doc.DocumentElement.SelectSingleNode("SmtpServer").InnerText; smtpSetting.Authentication = Convert.ToBoolean(doc.DocumentElement.SelectSingleNode("Authentication").InnerText); smtpSetting.From = doc.DocumentElement.SelectSingleNode("From").InnerText; smtpSetting.Password = doc.DocumentElement.SelectSingleNode("Pwd").InnerText; smtpSetting.ShowName = doc.DocumentElement.SelectSingleNode("ShowName").InnerText; smtpSetting.SmtpPort = Common.GetInt(doc.DocumentElement.SelectSingleNode("SmtpPort").InnerText); smtpSetting.IsEnable = Common.GetBool(doc.DocumentElement.SelectSingleNode("Enable").InnerText); return smtpSetting; } } #region 郵件發送 /// <summary> /// 發送郵件 /// </summary> /// <param name="to">收件者地址</param> /// <param name="title">標題</param> /// <param name="body">內容</param> /// <returns></returns> public static bool SendMail(string to, string title, string body, string attachPath) { bool flag = false; try { body = string.Format("{0}<br/><br/><span style='color:#f00;font-size:15px;'>此郵件由系統發出,無需直接回複!<a href='{1}' target='_blank'>{1}</a></span>", body, System.Web.Configuration.WebConfigurationManager.AppSettings["webDomain"]); MyEmail email = new MyEmail(GetMailSetting.Server, to, GetMailSetting.From, title, body, GetMailSetting.ShowName, GetMailSetting.From, GetMailSetting.Password, Convert.ToString(GetMailSetting.SmtpPort), false, false); email.AddAttachments(attachPath); email.Send(); flag = true; } catch (Exception ex) { //Console.WriteLine(ex.ToString()); } return flag; } #endregion } #region 郵件配置參數實體 /// <summary> /// 郵件配置參數實體 /// </summary> public class MailSetting { /// <summary> /// 是否啟用郵件 /// </summary> public bool IsEnable { get; set; } /// <summary> /// 連接埠 /// </summary> public int SmtpPort { get; set; } /// <summary> /// 郵件伺服器地址 /// </summary> public string Server { get; set; } /// <summary> /// 是否驗證寄件者使用者名稱和密碼 /// </summary> public bool Authentication { get; set; } /// <summary> /// 寄件者地址 /// </summary> public string From { get; set; } /// <summary> /// 寄件者地址的別名 /// </summary> public string ShowName { get; set; } /// <summary> /// 寄件者密碼 /// </summary> public string Password { get; set; } } #endregion}
<?xml version="1.0" encoding="utf-8" ?><Mail> <Enable>false</Enable> <SmtpServer>xxxxxxx</SmtpServer> <From>xxx@xxx.com</From> <Pwd>xxx</Pwd> <SmtpPort>25</SmtpPort> <ShowName>xxxx</ShowName> <Authentication>false</Authentication></Mail>