您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用C#.NET實現發送郵件功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么用C#.NET實現發送郵件功能”吧!
復制代碼 代碼如下:
using System; using System.Collections.Generic; using System.Text; using System.Net.Mail; using System.Net; namespace MyQuery.Utils { /// <summary> /// 封裝郵件處理 /// by 賈世義 2011-6-3 /// </summary> public static class MailHelper { private static string smtpHost = null; private static int smptPort = 25; private static bool smtpIsUserCredentials = false; private static string smtpCredentialAccount = null; private static string smtpCredentialPassword = null; /// <summary> /// 設置發送郵件參數 /// </summary> /// <param name="host">smtp服務器地址或名稱</param> /// <param name="port">smtp服務端口 一般為25</param> /// <param name="isUserCredentials">是否需要認證</param> /// <param name="account">需要認證時的用戶</param> /// <param name="password">需要認證時的用戶的密碼</param> public static void SetParameters(string host, int port, bool isUserCredentials, string account, string password) { smtpHost = host; smptPort = port; smtpIsUserCredentials = isUserCredentials; smtpCredentialAccount = account; smtpCredentialPassword = password; } /// <summary> /// 設置發送郵件參數 取配置 /// </summary> private static void setParameters() { if (String.IsNullOrEmpty(smtpHost)) { smtpHost = WebHelper.GetAppConfig("SmtpHost"); smptPort = DataHelper.GetIntValue(WebHelper.GetAppConfig("SmptPort"), 25); smtpIsUserCredentials = Constants.TRUE_ID.Equals(WebHelper.GetAppConfig("SmtpIsUserCredentials")); smtpCredentialAccount = WebHelper.GetAppConfig("SmtpCredentialAccount"); smtpCredentialPassword = WebHelper.GetAppConfig("SmtpCredentialPassword"); } } /// <summary> /// 發送郵件 發送郵件錯誤不會拋出異常 /// </summary> /// <param name="receivers">收件人</param> /// <param name="title">標題/主題</param> /// <param name="content">信件內容</param> /// <param name="sender">發件人 空則取系統配置</param> public static void SendMail(string receivers, string title, string content, string sender) { if (!String.IsNullOrEmpty(receivers)) { //初始化參數 setParameters(); if (!String.IsNullOrEmpty(smtpHost)) { try { SmtpClient smtp = new SmtpClient(smtpHost, smptPort); if (smtpIsUserCredentials) { smtp.UseDefaultCredentials = true; smtp.Credentials = new NetworkCredential(smtpCredentialAccount, smtpCredentialPassword); ; } smtp.DeliveryMethod = SmtpDeliveryMethod.Network; if (String.IsNullOrEmpty(sender)) { sender = smtpCredentialAccount; } foreach (string receiver in DataHelper.GetStrings(receivers)) { MailMessage msg = new MailMessage(sender, receiver, title, content); msg.BodyEncoding = Encoding.UTF8; msg.SubjectEncoding = Encoding.UTF8; msg.IsBodyHtml = true; smtp.Send(msg); msg.Dispose(); } } catch { } } } } } }
感謝各位的閱讀,以上就是“怎么用C#.NET實現發送郵件功能”的內容了,經過本文的學習后,相信大家對怎么用C#.NET實現發送郵件功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。