最近根據公司需求,寫個郵件發送。 這里面的傳入的地址信息的參數都是經過加密的。 主要是保證用戶信息的安全。
幫助類
1 using System; 2 using System.Collections.Generic; 3 using System.Configuration; 4 using System.IO; 5 using System.Linq; 6 using System.Net.Mail; 7 using System.Text; 8 using System.Web; 9 10 namespace CalslNum.Helper 11 { 12 /// <summary> 13 ///發送郵件類 14 /// </summary> 15 public class MailService 16 { 17 /// <summary> 18 /// 發送郵件程序調用方法 SendMail("abc@126.com", "某某人", "cba@126.com", "你好", "我測試下郵件", "郵箱登錄名", "郵箱密碼", "smtp.126.com", true,); 19 /// </summary> 20 /// <param name="from">發送人郵件地址</param> 21 /// <param name="fromname">發送人顯示名稱</param> 22 /// <param name="to">發送給誰(郵件地址)</param> 23 /// <param name="subject">標題</param> 24 /// <param name="body">內容</param> 25 /// <param name="username">郵件登錄名</param> 26 /// <param name="password">郵件密碼</param> 27 /// <param name="server">郵件服務器 smtp服務器地址</param> 28 /// <param name= "IsHtml "> 是否是HTML格式的郵件 </param> 29 /// <returns>send ok</returns> 30 public static bool SendMail(string from, string fromname, string to, string subject, string body, string server, string username, string password, bool IsHtml) 31 { 32 //郵件發送類 33 MailMessage mail = new MailMessage(); 34 try 35 { 36 //是誰發送的郵件 37 mail.From = new MailAddress(from, fromname); 38 //發送給誰 39 mail.To.Add(to); 40 //標題 41 mail.Subject = subject; 42 //內容編碼 43 mail.BodyEncoding = Encoding.Default; 44 //發送優先級 45 mail.Priority = MailPriority.High; 46 //郵件內容 47 mail.Body = body; 48 //是否HTML形式發送 49 mail.IsBodyHtml = IsHtml; 50 //郵件服務器和端口 51 SmtpClient smtp = new SmtpClient(server, 25); 52 smtp.UseDefaultCredentials = true; 53 //指定發送方式 54 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 55 //發件人身份驗證,否則163 發不了 56 smtp.UseDefaultCredentials = true; 57 //指定登錄名和密碼 58 smtp.Credentials = new System.Net.NetworkCredential(username, password); 59 //超時時間 60 smtp.EnableSsl = false; 61 smtp.Timeout = 10000; 62 smtp.Send(mail); 63 return true; 64 } 65 catch (Exception) 66 { 67 return false; 68 } 69 finally 70 { 71 mail.Dispose(); 72 } 73 } 74 75 //讀取指定URL地址的HTML,用來以后發送網頁用 76 public static string ScreenScrapeHtml(string url) 77 { 78 //讀取stream並且對於中文頁面防止亂碼 79 StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8); 80 string str = reader.ReadToEnd(); 81 reader.Close(); 82 return str; 83 } 84 85 //發送plaintxt 86 public static bool SendText(string from, string fromname, string to, string subject, string body, string server, string username, string password) 87 { 88 return SendMail(from, fromname, to, subject, body, server, username, password, false); 89 } 90 91 //發送HTML內容 92 public static bool SendHtml(string from, string fromname, string to, string subject, string body, string server, string username, string password) 93 { 94 return SendMail(from, fromname, to, subject, body, server, username, password, true); 95 } 96 97 //發送制定網頁 98 public static bool SendWebUrl(string from, string fromname, string to, string subject, string server, string username, string password, string url) 99 { 100 //發送制定網頁 101 return SendHtml(from, fromname, to, subject, ScreenScrapeHtml(url), server, username, password); 102 103 } 104 //默認發送格式 105 public static bool SendEmailDefault(string ToEmail,string f_username,string f_pass,string f_times) 106 { 107 StringBuilder MailContent = new StringBuilder(); 108 MailContent.Append("親愛的×××會員:<br/>"); 109 MailContent.Append(" 您好!你於"); 110 MailContent.Append(DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss")); 111 MailContent.Append("通過<a href='#'>×××</a>管理中心審請找回密碼。<br/>"); 112 MailContent.Append(" 為了安全起見,請用戶點擊以下鏈接重設個人密碼:<br/><br/>"); 113 string url = "http://www.×××.×××/SignIn/Rest?u=" + f_username + "&s=" + f_pass + "&t=" + f_times; 114 MailContent.Append("<a href='" + url + "'>" + url + "</a><br/><br/>"); 115 MailContent.Append(" (如果無法點擊該URL鏈接地址,請將它復制並粘帖到瀏覽器的地址輸入框,然后單擊回車即可。)"); 116 return SendHtml(ConfigurationManager.AppSettings["EmailName"].ToString(), "會員管理中心", ToEmail, "×××找回密碼", MailContent.ToString(), ConfigurationManager.AppSettings["EmailService"].ToString(), ConfigurationManager.AppSettings["EmailName"].ToString(), ConfigurationManager.AppSettings["EmailPass"].ToString()); //這是從webconfig中自己配置的。 117 } 118 } 119 }
webconfig配置信息
1 <add key="EmailName" value="××××@163.com"/> 2 <add key="EmailPass" value="××××"/> 3 <add key="EmailService" value="smtp.163.com"/> 4 5 //說明: 這里面的"EmailService"得與你自己設置郵箱的smtp/POP3/...服務要相同, 大部分是根據@后面的進行配置。我是用163郵箱配置的。 可以根據自己需要自己配置。
后台調用的方法
1 public ActionResult SendEmail(string EmailName) 2 { 3 EmailName = Helper.FI_DesTools.DesDecrypt(EmailName); 4 if (!Regex.IsMatch(EmailName, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) 5 { 6 return Content("0"); 7 } 8 string f_username = ""; 9 string f_pass = ""; 10 string f_times = Helper.FI_DesTools.DesEncrypt(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 11 List<user> list = (from a in users where a.emailaddress == EmailName select a).ToList(); 12 if (list.Count > 0) 13 { 14 f_username = Helper.FI_DesTools.DesEncrypt(list[0].×××); 15 f_pass = Helper.FI_DesTools.DesEncrypt(list[0].×××); 16 17 bool flag = Helper.MailService.SendEmailDefault(EmailName, “×××”,“×××”, “×××”); //這里面的參數根據自己需求自己定,最好進行加密 18 if (flag) 19 { 20 return Content("true"); 21 } 22 else 23 { 24 return Content("false"); 25 } 26 } 27 else { 28 return Content("false"); 29 } 30 }
發送完郵件效果圖如下: