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

C#简单邮件群发通用类

程序员文章站 2022-05-14 17:39:21
本文实例为大家介绍了c#邮件群发通用类,供大家参考,具体内容如下 public static class email { ///

本文实例为大家介绍了c#邮件群发通用类,供大家参考,具体内容如下

public static class email
  {
    /// <summary>
    /// 发件人
    /// </summary>
    public static string mailfrom { get; set; }

    /// <summary>
    /// 收件人
    /// </summary>
    public static string[] mailtoarray { get; set; }

    /// <summary>
    /// 抄送
    /// </summary>
    public static string[] mailccarray { get; set; }

    /// <summary>
    /// 标题
    /// </summary>
    public static string mailsubject { get; set; }

    /// <summary>
    /// 正文
    /// </summary>
    public static string mailbody { get; set; }

    /// <summary>
    /// 发件人密码
    /// </summary>
    public static string mailpwd { get; set; }

    /// <summary>
    /// smtp邮件服务器
    /// </summary>
    public static string host { get; set; }  

    /// <summary>
    /// 邮件服务器端口
    /// </summary>
    public static int port { get; set; }

    /// <summary>
    /// 正文是否是html格式
    /// </summary>
    public static bool isbodyhtml { get; set; }

    /// <summary>
    /// 附件
    /// </summary>
    public static string[] attachmentspath { get; set; }

    public static bool send()
    {
      //使用指定的邮件地址初始化mailaddress实例
      mailaddress maddr = new mailaddress(mailfrom);

      //初始化mailmessage实例
      mailmessage mymail = new mailmessage();

      //向收件人地址集合添加邮件地址
      if (mailtoarray != null)
      {
        for (int i = 0; i < mailtoarray.length; i++)
        {
          mymail.to.add(mailtoarray[i].tostring());
        }
      }

      //向抄送收件人地址集合添加邮件地址
      if (mailccarray != null)
      {
        for (int i = 0; i < mailccarray.length; i++)
        {
          mymail.cc.add(mailccarray[i].tostring());
        }
      }
      //发件人地址
      mymail.from = maddr;

      //电子邮件的标题
      mymail.subject = mailsubject;

      //电子邮件的主题内容使用的编码
      mymail.subjectencoding = encoding.utf8;

      //电子邮件正文
      mymail.body = mailbody;

      //电子邮件正文的编码
      mymail.bodyencoding = encoding.default;

      //电子邮件优先级
      mymail.priority = mailpriority.high;

      //电子邮件不是html格式
      mymail.isbodyhtml = isbodyhtml;

      //在有附件的情况下添加附件
      try
      {
        if (attachmentspath != null && attachmentspath.length > 0)
        {
          attachment attachfile = null;
          foreach (string path in attachmentspath)
          {
            attachfile = new attachment(path);
            mymail.attachments.add(attachfile);
          }
        }
      }
      catch (exception err)
      {
        throw new exception("在添加附件时有错误:" + err.message);
      }

      smtpclient client = new smtpclient();

      //指定发件人的邮件地址和密码以验证发件人身份
      client.credentials = new networkcredential(mailfrom, mailpwd);

      //设置smtp邮件服务器
      //client.host = "smtp." + mymail.from.host;
      client.host = host;

      //smtp邮件服务器端口
      client.port = port;

      //是否使用安全连接
      //client.enablessl = true;

      try
      {
        //将邮件发送到smtp邮件服务器
        client.send(mymail);
        return true;
      }
      catch (smtpexception ex)
      {
        string msg = ex.message;
        return false;
      }

    }

希望本文所述对大家学习c#程序设计有所帮助。