程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#通用郵件發送類分享

C#通用郵件發送類分享

編輯:C#入門知識

C#通用郵件發送類分享。本站提示廣大學習愛好者:(C#通用郵件發送類分享)文章只能為提供參考,不一定能成為您想要的結果。以下是C#通用郵件發送類分享正文


此類的功效包含發送郵件,郵箱格局能否准確,和在不發送郵件的情形下斷定郵箱用戶名和暗碼能否准確,鑒於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;}     
    } 
  }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved