因為之後會擴充因此還是參考網路上的文章寫一次,這隻功能會用到SmtpClient、MailMessage這兩個物件,對於System.Web.Mail的屬性方法已經不再支持因此改用System.Net.Mail(https://msdn.microsoft.com/zh-tw/library/system.web.mail.mailmessage(v=vs.80).aspx)。
實作會新增以下檔案:
- MailTemplate.cs//Mail屬性
- MailToolVariable.cs//取得Mail相關設定封裝
- Variable.cs//取得Web.config appSettings 封裝
- MailTool.cs//功能實作
呼叫使用方法
//準備資料 MailTemplate mailTemplate = new MailTemplate(); mailTemplate.MailSubject = "信件主旨"; mailTemplate.MailBody = "信件內容"; mailTemplate.MailTos = new string[] { "Test@test.com.tw" };//收件人 //寄信 MailTool StartSendMail = new MailTool(); StartSendMail.StartSendMail(mailTemplate);
實作:
新增MailTemplate.cs
public class MailTemplate { #region Mail /// <summary> /// 收信人E-mail Address /// </summary> public string[] MailTos { get; set; } /// <summary> /// 副本E-mail Address /// </summary> public string[] CCs { get; set; } /// <summary> /// 主旨 /// </summary> public string MailSubject { get; set; } /// <summary> /// 信件內容 /// </summary> public string MailBody { get; set; } /// <summary> /// 是否採用HTML格式 /// </summary> public bool IsBodyHtml { get; set; } /// <summary> /// 是否刪除在WebServer上的附件 /// </summary> public bool DeleteFileAttachment { get; set; } /// <summary> /// 是否使用 Secure Sockets Layer (SSL) 加密連線 /// </summary> public bool EnableSsl { get; set; } /// <summary> /// 附件檔案 /// </summary> public Dictionary<string, Stream> Files { get; set; } #endregion }
新增MailToolVariable.cs
public class MailToolVariable { /// <summary> /// 寄信密碼 /// </summary> public static string MailPwd { get { return Variable.GetAppSetting("MailPwd"); } } /// <summary> /// 寄信帳號 /// </summary> public static string MailAccount { get { return Variable.GetAppSetting("MailAccount"); } } /// <summary> /// 是否使用 Secure Sockets Layer (SSL) 加密連線 /// </summary> public static string EnableSsl { get { return Variable.GetAppSetting("EnableSsl"); } } /// <summary> /// 寄信smtp server的Port,預設25 /// </summary> public static int SmtpPort { get { return 25; } } /// <summary> /// 寄信smtp server /// </summary> public static string SmtpServer { get { return Variable.GetAppSetting("SmtpServer"); } } /// <summary> /// 寄信人E-mail Address /// </summary> public static string MailFrom { get { return Variable.GetAppSetting("MailFrom"); } } }
新增Variable.cs
public static string GetAppSetting(string Key) { string AppSetting = string.Empty; AppSetting = System.Configuration.ConfigurationManager.AppSettings[Key]; return AppSetting; }
web.configr加入
<appSettings> <!--Mail Server--> <add key="SmtpServer" value="Mail Server"/> <add key="MailFrom" value="寄件人E-Mail"/> <add key="MailAccount" value="寄件人名稱"/> <add key="MailPwd" value="寄件人密碼"/> <!--要:true不要:false 是否使用 Secure Sockets Layer (SSL) 加密連線--> <add key="EnableSsl" value="false"/> </appSettings>
Send Mail 功能實作
/// <summary> /// 寄信 /// </summary> /// <param name="mailTemplate">MailTemplate</param> /// <returns>>/returns> public bool StartSendMail(MailTemplate mailTemplate) { #region Send Mail try { //沒給寄信人 if (string.IsNullOrEmpty(MailToolVariable.MailFrom)) { return false; } //命名空間: System.Web.Mail已過時,http://msdn.microsoft.com/zh-tw/library/system.web.mail.mailmessage(v=vs.80).aspx MailMessage mms = new MailMessage(); //指定一位寄信人MailAddress mms.From = new MailAddress(MailToolVariable.MailFrom); //信件主旨 mms.Subject = mailTemplate.MailSubject; //信件內容 mms.Body = mailTemplate.MailBody; //信件內容 是否採用Html格式 mms.IsBodyHtml = mailTemplate.IsBodyHtml; //信件標題編碼 mms.SubjectEncoding = System.Text.Encoding.UTF8; if (mailTemplate.MailTos != null) { for (int i = 0; i < mailTemplate.MailTos.Length; i++) { //加入信件的收信人(們)address if (!string.IsNullOrEmpty(mailTemplate.MailTos[i].Trim())) { mms.To.Add(new MailAddress(mailTemplate.MailTos[i].Trim())); } } } if (mailTemplate.CCs != null) { for (int i = 0; i < mailTemplate.CCs.Length; i++) { if (!string.IsNullOrEmpty(mailTemplate.CCs[i].Trim())) { //加入信件的副本(們)address mms.CC.Add(new MailAddress(mailTemplate.CCs[i].Trim())); } } } //附件處理 if (mailTemplate.Files != null && mailTemplate.Files.Count > 0) { foreach (string fileName in mailTemplate.Files.Keys) { Attachment attfile = new Attachment(mailTemplate.Files[fileName], fileName); mms.Attachments.Add(attfile); } } SmtpClient client = new SmtpClient(MailToolVariable.SmtpServer, MailToolVariable.SmtpPort); //是否使用 Secure Sockets Layer (SSL) 加密連線 mailTemplate.EnableSsl = Convert.ToBoolean(MailToolVariable.EnableSsl); client.EnableSsl = mailTemplate.EnableSsl; //是否需要帳號密碼要看smtp server的設定 if (!string.IsNullOrEmpty(MailToolVariable.MailAccount) && !string.IsNullOrEmpty(MailToolVariable.MailPwd))//.config有帳密的話 { client.Credentials = new NetworkCredential(MailToolVariable.MailAccount, MailToolVariable.MailPwd);//寄信帳密 } client.Send(mms);//寄出一封信 #endregion //釋放每個附件,才不會Lock住 if (mms.Attachments != null && mms.Attachments.Count > 0) { for (int i = 0; i < mms.Attachments.Count; i++) { mms.Attachments[i].Dispose(); mms.Attachments[i] = null; } } return true; } catch (Exception ex) { return false; } }
參考:
.Net 完整的Mail寄信(Send Mail)功能
檔案上傳後刪除的問題
沒有留言:
張貼留言