欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#通用邮件发送类分享

程序员文章站 2023-01-22 14:05:06
此类的功能包括发送邮件,邮箱格式是否正确,和在不发送邮件的情况下判断邮箱用户名和密码是否正确,鉴于pop检查邮箱用户名和密码出现错误情况返回结果的延迟,用异步线程解决此问题...

此类的功能包括发送邮件,邮箱格式是否正确,和在不发送邮件的情况下判断邮箱用户名和密码是否正确,鉴于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;}     
    } 
  }
}