C#
C# 郵件系統 HTML方式 多語系
只要抽換相對應的HTML就是一個新的郵件
zh-TW_Test001.html
zh-CN_Test001.html
en-US_Test001.html
zh-TW_Test002.html
zh-CN_Test002.html
en-US_Test002.html
方法如下需要幾個檔案
zh-TW_Test001.html
EmailReplaceModel.cs
SendEmail.cs
MailReadFile.cs
//呼叫範例
SendEmail SendEmailCEA = new SendEmail();
EmailReplaceModel EmailReplace = new EmailReplaceModel()
{
NameRecipient = "Tree",
account = "TreeTest@xxx.com"
};
SendEmailCEA.SendMail("Tree@xxx.com.tw", "新帳號建立確認", "Test001", "zh-TW", EmailReplace);
<!--html範例-->
<!--zh-TW_Test001.html-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<meta charset='utf-8' />
</head>
<body>
Dear #name#
<br> zh-TW_Test001
<br> 帳號 => #account#
<br> <a href='https://translate.google.com.tw/'>翻譯網址</a>
</body>
</html>
//EmailReplaceModel.cs
public class EmailReplaceModel
{
public string NameRecipient { get; set; } //收件人
public string account { get; set; } //帳號
//...
//...
//無限向下增加
}
//MailReadFile.cs
public class MailReadFile
{
//讀取信件範本
//輸入
// sHtmlName : 郵件名稱
//輸出 => 範本郵件內容
public string getEmailData(string sHtmlName, string language)
{
//輸出
string returnData = "";
//實體路徑的html檔 = 應用程式所在的目錄 + 所在頁面
string path = System.AppDomain.CurrentDomain.BaseDirectory + @"\Emailxxxx\" + language + @"_" + sHtmlName + @".html";
//有資料
if (File.Exists(path))
{
//讀取檔案
StreamReader streamReader = new StreamReader(path, Encoding.GetEncoding("Big5"));
//轉字串給輸出
returnData = streamReader.ReadToEnd();
}
return returnData;
}
//取代郵件文字
//輸入
// sHtmlName : 郵件名稱
// mailReplaceData : 輸入參數
//輸出 => 完整郵件內容
public string setReplacedEmailData(string sHtmlName, string language, EmailReplaceModel mailReplaceData)
{
//輸出
string returnData = "";
//讀取信件範本
returnData = getEmailData(sHtmlName, language);
//取代文字
if (mailReplaceData != null)
{
if (!string.IsNullOrEmpty(mailReplaceData.NameRecipient))
{
returnData = Regex.Replace(returnData, "#name#", mailReplaceData.NameRecipient);
}
if (!string.IsNullOrEmpty(mailReplaceData.account))
{
returnData = Regex.Replace(returnData, "#account#", mailReplaceData.account);
}
//...無限向下增加
}
return returnData;
}
}
//SendEmail.cs
public class SendEmail
{
//c# 設定郵件伺服器 發送email
//MailList : 收件者 EX:"Tree@xxx.com.tw";
//Subject : 主旨
//mailName : 信件範本
//language : 語系
//mailReplaceData : 範本文字取代資料
public void SendMail(string mailList, string subject, string mailName, string language, EmailReplaceModel mailReplaceData)
{
//讀取信件範本並且取代正確的資訊
MailReadFile gatMailHtml = new MailReadFile();
var body = gatMailHtml.setReplacedEmailData(mailName, language, mailReplaceData);
//寄信
SendMailPrivate(mailList, subject, body);
}
//MailList : 收件者 EX:"Tree@xxx.com.tw";
//Subject : 主旨
//Body : 內容
private void SendMailPrivate(string mailList, string subject, string body)
{
MailMessage mail = new MailMessage();
//設定寄件者
mail.From = new MailAddress("xxxServer@xxx.com.tw");
//主旨
mail.Subject = subject;
//內容
mail.Body = body;
//內容格式
mail.IsBodyHtml = true;
//收件者
mail.To.Add(mailList);//收件者
//mail.To.Add("Tree@xxx.com.tw,Tree2@xxx.com.tw");//以逗號分隔
//mail.Bcc.Add("Tree@xxx.com.tw"); //密件副本收件者
//mail.CC.Add("Tree@xxx.com.tw"); //副本收件者
//加入附件
//Attachment attch = new Attachment("C:\1.txt");
//mail.Attachments.Add(attch);
SmtpClient smtpclient = new SmtpClient();
smtpclient.Host = WebConfigurationManager.AppSettings["EmailHost"]; //設定SMTP Server EX: xxx.xxx.com
smtpclient.Port = WebConfigurationManager.AppSettings["EmailPort"]; //設定Port EX: 25
smtpclient.Send(mail);
}
}
我只是一棵樹