C#.NET采用HTML模板發(fā)送電子郵件完整實例
本文實例講述了C#.NET采用HTML模板發(fā)送電子郵件的方法,是非常實用的技巧。分享給大家供大家參考。具體方法如下:
要使用html模板進行發(fā)送郵件,需要準(zhǔn)備以下幾項工作:
1)HTML模板
2)替換函數(shù)(替換模板中綁定的變量)
3)郵件函數(shù)(發(fā)送郵件)
一、HTML模板
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>HTML Report</title> </head> <body> <p>$USER_NAME$:</p> <p>My name is $NAME$</p> <p >This is a Test Email,<br /> $MY_NAME$</p> </body> </html>
其中USER_NAME、NAME、MY_NAME這三個變量用$符號包裹進行標(biāo)識,是需要被替換的字符串,它會在下面的替換函數(shù)中被動態(tài)替換。
二、替換函數(shù)
/// <summary> ///替換模板中的字段值 /// </summary> public string ReplaceText(String userName,string name,string myName) { string path = string.Empty; path = HttpContext.Current.Server.MapPath("EmailTemplate\\emailTemplate.html"); if (path == string.Empty) { return string.Empty; } System.IO.StreamReader sr = new System.IO.StreamReader(path); string str = string.Empty; str = sr.ReadToEnd(); str = str.Replace("$USER_NAME$", userName); str = str.Replace("$NAME$", name); str = str.Replace("$MY_NAME$",myName); return str; }
三、郵件發(fā)送
/// <summary> /// 發(fā)送郵件 /// </summary> public void SendEmail(string email_from,string email_to, string email_cc, string userName, string name, string myName) { try { // 建立一個郵件實體 MailAddress from = new MailAddress(email_from); MailAddress to = new MailAddress(email_to); MailMessage message = new MailMessage(from, to); string strbody = ReplaceText(userName, name, myName); if (email_cc.ToString() != string.Empty) { foreach (string ccs in email_cc.Split(';')) { MailAddress cc = new MailAddress(ccs); message.CC.Add(cc); } } message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; message.Priority = MailPriority.High; message.Body = strbody; //郵件BODY內(nèi)容 message.Subject = "Subject"; SmtpClient smtp = new SmtpClient(); smtp.Host = Configuration.MailHost; smtp.Port = Configuration.MailHostPort; smtp.Credentials = new System.Net.NetworkCredential(email_from, "emailpassword"); smtp.Send(message); //發(fā)送郵件 } catch (Exception ex) { throw ex; } }
其實無論采取什么方式或組件進行郵件發(fā)送,要替換HTML模板中的內(nèi)容,只需一個Replace函數(shù)即可。
相信本文所述對大家C#.net程序設(shè)計的學(xué)習(xí)有一定的借鑒價值。
相關(guān)文章
FTPClientHelper輔助類 實現(xiàn)文件上傳,目錄操作,下載等操作
這篇文章主要分享了一個FTPClientHelper輔助類和介紹了常用的FTP命令,需要的朋友可以參考下。2016-06-06使用C#實現(xiàn)阿拉伯?dāng)?shù)字到大寫中文的轉(zhuǎn)換
這篇文章主要介紹了C#實現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)為大寫中文的實現(xiàn)代碼,需要的朋友可以參考下2007-03-03C#實現(xiàn)集合轉(zhuǎn)換成json格式數(shù)據(jù)的方法
這篇文章主要介紹了C#實現(xiàn)集合轉(zhuǎn)換成json格式數(shù)據(jù)的方法,涉及C#針對dataTable、Enumerable及Json格式數(shù)據(jù)的遍歷及轉(zhuǎn)換操作相關(guān)技巧,需要的朋友可以參考下2016-07-07C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
在工作中經(jīng)常遇到C#數(shù)組、ArrayList、List、Dictionary存取數(shù)據(jù),但是該選擇哪種類型進行存儲數(shù)據(jù)呢?很迷茫,今天小編抽空給大家整理下這方面的內(nèi)容,需要的朋友參考下吧2017-02-02C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)示例代碼
這篇文章主要給大家介紹了關(guān)于C#如何使用Bogus創(chuàng)建模擬數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04