這篇文章主要爲大家詳細介紹了C# Email郵件發送功能,找回或重置密碼功能,具有一定的參考價值,感興趣的小夥伴們可以參考一下
最近根據公司需求,寫個郵件發送。這裏面的傳入的地址信息的參數都是經過加密的,主要是保證用戶信息的安全。
using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Net.Mail; using System.Text; using System.Web; namespace CalslNum.Helper { /// <summary> ///發送郵件類 /// </summary> public class MailService { /// <summary> /// 發送郵件程序調用方法 SendMail("[email protected]", "某某人", "[email protected]", "你好", "我測試下郵件", "郵箱登錄名", "郵箱密碼", "smtp.126.com", true,); /// </summary> /// <param name="from">發送人郵件地址</param> /// <param name="fromname">發送人顯示名稱</param> /// <param name="to">發送給誰(郵件地址)</param> /// <param name="subject">標題</param> /// <param name="body">內容</param> /// <param name="username">郵件登錄名</param> /// <param name="password">郵件密碼</param> /// <param name="server">郵件服務器 smtp服務器地址</param> /// <param name= "IsHtml "> 是否是HTML格式的郵件 </param> /// <returns>send ok</returns> public static bool SendMail(string from, string fromname, string to, string subject, string body, string server, string username, string password, bool IsHtml) { //郵件發送類 MailMessage mail = new MailMessage(); try { //是誰發送的郵件 mail.From = new MailAddress(from, fromname); //發送給誰 mail.To.Add(to); //標題 mail.Subject = subject; //內容編碼 mail.BodyEncoding = Encoding.Default; //發送優先級 mail.Priority = MailPriority.High; //郵件內容 mail.Body = body; //是否HTML形式發送 mail.IsBodyHtml = IsHtml; //郵件服務器和端口 SmtpClient smtp = new SmtpClient(server, 25); smtp.UseDefaultCredentials = true; //指定發送方式 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //發件人身份驗證,否則163 發不了 smtp.UseDefaultCredentials = true; //指定登錄名和密碼 smtp.Credentials = new System.Net.NetworkCredential(username, password); //超時時間 smtp.EnableSsl = false; smtp.Timeout = 10000; smtp.Send(mail); return true; } catch (Exception) { return false; } finally { mail.Dispose(); } } //讀取指定URL地址的HTML,用來以後發送網頁用 public static string ScreenScrapeHtml(string url) { //讀取stream並且對於中文頁面防止亂碼 StreamReader reader = new StreamReader(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream(), System.Text.Encoding.UTF8); string str = reader.ReadToEnd(); reader.Close(); return str; } //發送plaintxt public static bool SendText(string from, string fromname, string to, string subject, string body, string server, string username, string password) { return SendMail(from, fromname, to, subject, body, server, username, password, false); } //發送HTML內容 public static bool SendHtml(string from, string fromname, string to, string subject, string body, string server, string username, string password) { return SendMail(from, fromname, to, subject, body, server, username, password, true); } //發送制定網頁 public static bool SendWebUrl(string from, string fromname, string to, string subject, string server, string username, string password, string url) { //發送制定網頁 return SendHtml(from, fromname, to, subject, ScreenScrapeHtml(url), server, username, password); } //默認發送格式 public static bool SendEmailDefault(string ToEmail,string f_username,string f_pass,string f_times) { StringBuilder MailContent = new StringBuilder(); MailContent.Append("親愛的×××會員:<br/>"); MailContent.Append(" 您好!你於"); MailContent.Append(DateTime.Now.ToString("yyyy-MM-dd HH:MM:ss")); MailContent.Append("通過<a href='#'>×××</a>管理中心審請找回密碼。<br/>"); MailContent.Append(" 爲了安全起見,請用戶點擊以下鏈接重設個人密碼:<br/><br/>"); 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配置信息
<add key="EmailName" value="××××@163.com"/> <add key="EmailPass" value="××××"/> <add key="EmailService" value="smtp.163.com"/>
//說明: 這裏面的"EmailService"得與你自己設置郵箱的smtp/POP3/...服務要相同, 大部分是根據@後面的進行配置。我是用163郵箱配置的。 可以根據自己需要自己配置。
後臺調用的方法
public ActionResult SendEmail(string EmailName) { EmailName = Helper.FI_DesTools.DesDecrypt(EmailName); if (!Regex.IsMatch(EmailName, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")) { return Content("0"); } string f_username = ""; string f_pass = ""; string f_times = Helper.FI_DesTools.DesEncrypt(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); List<user> list = (from a in users where a.emailaddress == EmailName select a).ToList(); if (list.Count > 0) { f_username = Helper.FI_DesTools.DesEncrypt(list[0].×××); f_pass = Helper.FI_DesTools.DesEncrypt(list[0].×××); bool flag = Helper.MailService.SendEmailDefault(EmailName, “×××”,“×××”, “×××”); //這裏面的參數根據自己需求自己定,最好進行加密 if (flag) { return Content("true"); } else { return Content("false"); } } else { return Content("false"); } }
發送完郵件效果圖如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持神馬文庫。