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

C# 邮箱mail 发送类

程序员文章站 2023-11-13 13:34:10
没有牛b的设计模式,代码精练精练实用,功能齐全,调用简单 。。全全完完为码农考虑 mailsmtp ms = new mailsmtp("smtp.qq.com"...

没有牛b的设计模式,代码精练精练实用,功能齐全,调用简单 。。全全完完为码农考虑

mailsmtp ms = new mailsmtp("smtp.qq.com","1215247044","xxxx");
     //可选参数
     ms.setcc("610262374@qq.com");//抄送可以多个
     ms.setbc("610262374@qq.com");//暗送可以多个
     ms.setishtml(true);//默认:true
     ms.setencoding(system.text.encoding.utf8);//设置格式 默认utf-8
     ms.setisssl(true);//是否ssl加密 默认为false
     //调用函数
     bool issuccess = ms.send("1215247044@qq.com", "test", "610262374@qq.com", "哈哈", "哈哈", server.mappath("~/test.dll"));
     //输出结果
     response.write(ms.result); 
  

代码:

using system;
using system.io;
using system.web.ui.webcontrols;
using system.text;
using system.net.mail;
using system.net;
using system.linq;
using system.text.regularexpressions;
namespace syntacticsugar
{
  /// <summary>
  /// ** 描述:邮件发送
  /// ** 创始时间:2015-6-8
  /// ** 修改时间:-
  /// ** 作者:sunkaixuan
  /// </summary>
  public class mailsmtp
  {
    /// <summary>
    /// 设置邮件编码类型
    /// </summary>
    /// <param name="contentencoding"></param>
    public void setencoding(encoding contentencoding)
    {
      this._encoding = contentencoding;
    }
    /// <summary>
    ///设置邮件正文是否为 html 格式 
    /// </summary>
    /// <param name="ishtml"></param>
    public void setishtml(bool ishtml)
    {
      this._ishtml = ishtml;
    }
    /// <summary>
    /// 抄送
    /// </summary>
    /// <param name="cc"></param>
    public void setcc(params string[] cc)
    {
      this._cc = cc;
    }
    /// <summary>
    /// 暗送
    /// </summary>
    /// <param name="cc"></param>
    public void setbc(params string[] bc)
    {
      this._bcc = bc;
    }
    /// <summary>
    /// 是否ssl加密
    /// </summary>
    /// <param name="isssl"></param>
    public void setisssl(bool isssl)
    {
      this._smtp.enablessl = isssl;
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="host"></param>
    /// <param name="username">邮件账号</param>
    /// <param name="password">密码</param>
    public mailsmtp(string host, string username, string password)
    {
      this._smtp.host = host;
      this._smtp.port = 0x19;
      this._smtp.enablessl = false;
      this._ishtml = true;
      this._encoding = encoding.utf8;
      if (string.isnullorempty(username) || string.isnullorempty(password))
      {
        this._smtp.usedefaultcredentials = false;
      }
      else
      {
        this._smtp.credentials = new networkcredential(username, password);
      }
    }
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="from">发件人邮件地址</param>
    /// <param name="sender">发件人显示名称</param>
    /// <param name="to">收件人地址</param>
    /// <param name="subject">邮件标题</param>
    /// <param name="body">邮件正文</param>
    /// <param name="file">附件地址数组</param>
    /// <returns>bool 是否成功 </returns>
    public bool send(string from, string sender, string to, string subject, string body, params string[] file)
    {
      return send(from, sender, new string[] { to }, subject, body, file);
    }
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="from">发件人邮件地址</param>
    /// <param name="sender">发件人显示名称</param>
    /// <param name="to">收件人地址</param>
    /// <param name="subject">邮件标题</param>
    /// <param name="body">邮件正文</param>
    /// <param name="file">附件地址数组</param>
    /// <returns>bool 是否成功 </returns>
    public bool send(string from, string sender, string[] to, string subject, string body, params string[] file)
    {
      string mailreg = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
      if (to == null)
      {
        throw new argumentnullexception("mailsmtp.send.to");
      }
      if (to.any(oit => !regex.ismatch(oit + "", mailreg)))
      {
        this.result = "收件人地址不合法";
        return false;
      }
      if (_bcc != null && _bcc.length > 0)
      {
        if (_bcc.any(oit => !regex.ismatch(oit + "", mailreg)))
        {
          this.result = "暗送人地址不合法";
          return false;
        }
      }
      if (_cc != null && _cc.length > 0)
      {
        if (_cc.any(oit => !regex.ismatch(oit + "", mailreg)))
        {
          this.result = "抄送人地址不合法";
          return false;
        }
      }
      mailmessage message = new mailmessage();
      // 创建一个附件对象
      foreach (var r in file)
      {
        attachment objmailattachment;
        objmailattachment = new attachment(r);//发送邮件的附件
        message.attachments.add(objmailattachment);
      }
      message.from = new mailaddress(from, sender);
      message.subject = subject;
      message.subjectencoding = this._encoding;
      message.body = body;
      message.bodyencoding = this._encoding;
      message.isbodyhtml = this._ishtml;
      message.priority = mailpriority.normal;
      foreach (string str in to)
      {
        message.to.add(str);
      }
      if (this._bcc != null && this._bcc.length > 0)
      {
        foreach (string b in this._bcc)
        {
          message.bcc.add(b);
        }
      }
      if (this._cc != null && this._cc.length > 0)
      {
        foreach (string c in this._cc)
        {
          message.cc.add(c);
        }
      }
      try
      {
        this._smtp.send(message);
        return true;
      }
      catch (exception ex)
      {
        console.writeline(ex.message);
      }
      return false;
    }
    private smtpclient _smtp = new smtpclient();
    private encoding _encoding { get; set; }
    private bool _ishtml { get; set; }
    private string[] _cc { get; set; }
    private string[] _bcc { get; set; }
    /// <summary>
    /// 获取发送结果,成功则为空
    /// </summary>
    public string result { get; private set; }
  }
 
} 

以上所述就是本文的全部内容了,希望大家能够喜欢。