標籤:style blog http color os io
- 微軟封裝好的MailMessage類:主要處理髮送郵件的內容(如:收發人地址、標題、主體、圖片等等)
- 微軟封裝好的SmtpClient類:主要處理用smtp方式發送此郵件的配置資訊(如:郵件伺服器、傳送埠號、驗證方式等等)
- SmtpClient主要進行了三層的封裝:Socket --> TcpClient --> SmtpClient
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; //寄件匣的使用者名稱(即@符號前面的字串,例如:[email protected],使用者名稱為: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="username">寄件匣的使用者名稱(即@符號前面的字串,例如:[email protected],使用者名稱為: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 username, string password, string port, bool sslEnable, bool pwdCheckEnable) { try { mMailMessage = new MailMessage(); mMailMessage.To.Add(toMail); mMailMessage.From = new MailAddress(fromMail); 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()); } } public void Send() { try { if (mMailMessage != null) { mSmtpClient = new SmtpClient(); //mSmtpClient.Host = "smtp." + mMailMessage.From.Host; 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 = System.Net.Mail.SmtpDeliveryMethod.Network; mSmtpClient.Send(mMailMessage); } } catch (Exception ex) { //m_CreateErrorLogTxt("程式池(Email)通知", "郵件發送失敗", ex.Message); } } }
轉自:http://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/14/2212255.html
/// <summary> /// 發送郵件通知 /// </summary> /// <param name="subjectInfo">標題</param> /// <param name="bodyInfo">內容</param> public static void Email(string subjectInfo, string bodyInfo) { try { var appSettings = System.Configuration.ConfigurationManager.AppSettings;//擷取config配置資訊 string senderServerIp = "mail.zbian.cn"; string toMailAddress = appSettings["toMailAddress"];//接受方地址 string fromMailAddress = appSettings["fromMailAddress"];//發送方地址 string mailUsername = appSettings["mailUsername"]; //發送賬戶 string mailPassword = appSettings["mailPassword"]; //發送郵箱的密碼 string mailPort = "25"; MyEmail email = new MyEmail(senderServerIp, toMailAddress, fromMailAddress, subjectInfo, bodyInfo, mailUsername, mailPassword, mailPort, false, false); email.Send(); } catch (Exception ex) { } }
///添加config配置資訊 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings.Add("IsFirstInstall", "true"); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); ///擷取config配置資訊 var appSettings = System.Configuration.ConfigurationManager.AppSettings;