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

c# 郵件發送通用類

編輯:C#入門知識

此類的功能包括發送郵件,郵箱格式是否正確,和在不發送郵件的情況下判斷郵箱用戶名和密碼是否正確,鑒於POP檢查郵箱用戶名和密碼出現錯誤情況返回結果的延遲,用異步線程解決此問題,見代碼
[csharp]
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; 
            } www.2cto.com
        } 
         
        /// <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;}          
        }   
    } 

作者:kaoleba126com

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved