1. 程式人生 > >C# Email郵件傳送,功能是密碼找回或者重置功能。

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"); } }

傳送完郵件效果圖如下: