C#實現郵件發送功能

來源:互聯網
上載者:User

標籤: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;

 

                  

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.