C\#이메일 발송 기능 비밀번호 찾기 또는 리 셋 기능

7132 단어 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 }
웹 config 설정 정보

<add key="EmailName" value="××××@163.com"/>
<add key="EmailPass" value="××××"/>
<add key="EmailService" value="smtp.163.com"/>
//설명:이 안의"Email Service"는 당신 이 메 일 을 설정 한 smtp/POP 3/...서비스 와 같 아야 합 니 다.대부분@뒤의 설정 에 따라 설정 합 니 다.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");
   }
  }
메 일 발송 완료 효과 도 는 다음 과 같 습 니 다:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기