您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用C#.NET實(shí)現(xiàn)發(fā)送郵件功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么用C#.NET實(shí)現(xiàn)發(fā)送郵件功能”吧!
復(fù)制代碼 代碼如下:
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> /// 設(shè)置發(fā)送郵件參數(shù) /// </summary> /// <param name="host">smtp服務(wù)器地址或名稱</param> /// <param name="port">smtp服務(wù)端口 一般為25</param> /// <param name="isUserCredentials">是否需要認(rèn)證</param> /// <param name="account">需要認(rèn)證時(shí)的用戶</param> /// <param name="password">需要認(rèn)證時(shí)的用戶的密碼</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> /// 設(shè)置發(fā)送郵件參數(shù) 取配置 /// </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> /// 發(fā)送郵件 發(fā)送郵件錯(cuò)誤不會(huì)拋出異常 /// </summary> /// <param name="receivers">收件人</param> /// <param name="title">標(biāo)題/主題</param> /// <param name="content">信件內(nèi)容</param> /// <param name="sender">發(fā)件人 空則取系統(tǒng)配置</param> public static void SendMail(string receivers, string title, string content, string sender) { if (!String.IsNullOrEmpty(receivers)) { //初始化參數(shù) 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實(shí)現(xiàn)發(fā)送郵件功能”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么用C#.NET實(shí)現(xiàn)發(fā)送郵件功能這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。