国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#通用郵件發送類分享

2020-01-24 01:49:19
字體:
來源:轉載
供稿:網友

此類的功能包括發送郵件,郵箱格式是否正確,和在不發送郵件的情況下判斷郵箱用戶名和密碼是否正確,鑒于POP檢查郵箱用戶名和密碼出現錯誤情況返回結果的延遲,用異步線程解決此問題,見代碼:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;using System.Web;using System.Net;using System.Text.RegularExpressions;using System.Net.Sockets;using System.IO;using System.Collections;using System.Threading;namespace Com.Web{  /// <summary>  /// 郵箱類  /// </summary>  public class CheckEmailInfo  {    public string server { get; set; }//服務器    public string user { get; set; }//用戶名    public string pwd { get; set; }//密碼  }  /// <summary>  /// SendEmail通用類,通過smtp服務器發送郵件  /// </summary>  public class SendEmail  {    public Dictionary<string, string> smtpServer;    public Dictionary<string, string> popServer;    public SendEmail()    {           IniSmtpServer();      IniPopServer();    }    /// <summary>    /// 初始化常用smtpServer,用于綁定下拉選擇菜單    /// </summary>    private void IniSmtpServer()    {      smtpServer = new Dictionary<string, string>();      smtpServer.Add("網易163郵箱", "smtp.163.com");      smtpServer.Add("網易vip.163郵箱", "smtp.vip.163.com");      smtpServer.Add("網易126郵箱", "smtp.126.com");      smtpServer.Add("網易188郵箱", "smtp.188.com");      smtpServer.Add("新浪郵箱", "smtp.sina.com");      smtpServer.Add("雅虎郵箱", "smtp.mail.yahoo.com");      smtpServer.Add("搜狐郵箱", "smtp.sohu.com");      smtpServer.Add("TOM郵箱", "smtp.tom.com");      smtpServer.Add("Gmail郵箱", "smtp.gmail.com");      smtpServer.Add("QQ郵箱", "smtp.qq.com");      smtpServer.Add("QQ企業郵箱", "smtp.biz.mail.qq.com");      smtpServer.Add("139郵箱", "smtp.139.com");      smtpServer.Add("263郵箱", "smtp.263.com");          }    /// <summary>    /// 初始化常用popServer,用于綁定下拉選擇菜單    /// </summary>    private void IniPopServer()    {      popServer = new Dictionary<string, string>();      popServer.Add("網易163郵箱", "pop3.163.com");      popServer.Add("網易vip.163郵箱", "pop3.vip.163.com");      popServer.Add("網易126郵箱", "pop3.126.com");      popServer.Add("網易188郵箱", "pop3.188.com");      popServer.Add("新浪郵箱", "pop3.sina.com");      popServer.Add("雅虎郵箱", "pop3.mail.yahoo.com");      popServer.Add("搜狐郵箱", "pop3.sohu.com");      popServer.Add("TOM郵箱", "pop.tom.com");      popServer.Add("Gmail郵箱", "pop.gmail.com");      popServer.Add("QQ郵箱", "pop.qq.com");      popServer.Add("QQ企業郵箱", "pop.biz.mail.qq.com");      popServer.Add("139郵箱", "pop.139.com");      popServer.Add("263郵箱", "pop.263.com");    }    /// <summary>    /// 發送郵件功能    /// </summary>    /// <param name="fromEmail">登錄郵箱</param>    /// <param name="password">登錄密碼</param>    /// <param name="user">郵件昵稱</param>    /// <param name="title">郵件標題</param>    /// <param name="toEmail">郵件地址</param>    /// <param name="email">郵件內容</param>    /// <param name="smtpServer">smtp服務器</param>    public bool SendMessage(string fromEmail,string password, string user, string title, string toEmail, string email,string smtpServer)    {      try      {               SmtpClient smtp = new SmtpClient(); //實例化一個SmtpClient        smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //將smtp的出站方式設為 Network        smtp.EnableSsl = false;//smtp服務器是否啟用SSL加密        smtp.Host = smtpServer;//指定 smtp 服務器                  smtp.Credentials = new NetworkCredential(fromEmail, password);        MailMessage mm = new MailMessage(); //實例化一個郵件類        mm.Priority = MailPriority.High; //郵件的優先級,分為 Low, Normal, High,通常用 Normal即可               mm.From = new MailAddress(fromEmail, user, Encoding.GetEncoding(936));        mm.CC.Add(new MailAddress(toEmail, "", Encoding.GetEncoding(936)));        mm.Subject = title; //郵件標題        mm.SubjectEncoding = Encoding.GetEncoding(936);        mm.IsBodyHtml = true; //郵件正文是否是HTML格式mm.BodyEncoding = Encoding.GetEncoding(936);        mm.Body = email;        smtp.Send(mm);        return true;           }      catch      {        return false;      }    }    /// <summary>    /// 檢查郵箱是否正確的委托    /// </summary>    delegate bool MyDelegate(object checkEmailInfo);    /// <summary>    /// 利用異步方式檢查郵箱賬號和密碼是否正確    /// </summary>    public bool CheckUser(string server, string user, string pwd)    {            MyDelegate myDelegate = new MyDelegate(CheckUser);      CheckEmailInfo checkEmailInfo = new CheckEmailInfo();      checkEmailInfo.server = server;      checkEmailInfo.user = user;      checkEmailInfo.pwd = pwd;      IAsyncResult result = myDelegate.BeginInvoke(checkEmailInfo, null, null);      Thread.Sleep(1000);//主線程1秒后檢查異步線程是否運行完畢      if (result.IsCompleted)      { return myDelegate.EndInvoke(result); }//如果錯誤的郵箱和密碼,函數將會運行很慢      else      { return false; }    }       /// <summary>    /// 判斷用戶郵箱賬號和密碼是否正確    /// </summary>    /// <param name="server">PopServer地址</param>    /// <param name="user">用戶名</param>    /// <param name="pwd">密碼</param>    private bool CheckUser(object checkEmailInfo)    {           CheckEmailInfo checkInfo = (CheckEmailInfo)checkEmailInfo;      TcpClient sender = new TcpClient(checkInfo.server, 110);//pop協議使用TCP的110端口      Byte[] outbytes;      NetworkStream ns;      StreamReader sr;      string input;      string readuser = string.Empty;      string readpwd = string.Empty;      try      {        ns = sender.GetStream();        sr = new StreamReader(ns);        sr.ReadLine();        //檢查用戶名和密碼        input = "user " + checkInfo.user+ "/r/n";        outbytes = Encoding.ASCII.GetBytes(input.ToCharArray());        ns.Write(outbytes, 0, outbytes.Length);               readuser = sr.ReadLine();        input = "pass " + checkInfo.pwd + "/r/n";        outbytes =Encoding.ASCII.GetBytes(input.ToCharArray());        ns.Write(outbytes, 0, outbytes.Length);              readpwd = sr.ReadLine();                      if (readuser.Substring(0, 3) == "+OK" && readpwd.Substring(0, 3) == "+OK")        { return true; }        else        { return false; }      }      catch      {        return false;      }    }        /// <summary>    /// 判斷郵箱格式是否正確    /// </summary>    /// <param name="email">郵箱地址</param>    public bool IsEmail(string email)    {       string paterner = @"/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*";      if (!Regex.IsMatch(email, paterner))      { return false;}      else      {return true;}         }   }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平乡县| 赣州市| 桐城市| 清徐县| 札达县| 福鼎市| 宁强县| 黄山市| 独山县| 马关县| 五家渠市| 和田县| 琼结县| 通海县| 台北县| 青铜峡市| 阿合奇县| 丁青县| 远安县| 镇康县| 莆田市| 嵩明县| 巴楚县| 沈阳市| 浮梁县| 东源县| 镇远县| 米林县| 白水县| 贵州省| 荔波县| 濮阳县| 四平市| 法库县| 林芝县| 潞城市| 郴州市| 体育| 邢台县| 呼玛县| 宿州市|