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

C#实现的自定义邮件发送类完整实例(支持多人多附件)

程序员文章站 2022-07-06 12:50:49
本文实例讲述了c#实现的自定义邮件发送类。分享给大家供大家参考,具体如下: using system; using system.collections.gen...

本文实例讲述了c#实现的自定义邮件发送类。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.text;
using system.net;
using system.net.mail;
using system.net.mime;
namespace consoleapplication1
{
 /// <summary>
 /// 发送邮件类 的摘要说明
 /// </summary>
 class sendmail
 {
  #region 数据成员
  //收件人地址
  private string m_to = "";
  //发件人地址
  private string m_from = "";
  //邮件标题
  private string m_subject = "";
  //邮件正文
  private string m_body = "";
  //发送服务器名或地址
  private string m_host = "";
  //发件人用户名
  private string m_username = "";
  //发件人密码
  private string m_password = "";
  //邮件附件
  private string m_file = "";
  #endregion
  #region 构造函数
  /// <summary>
  /// 构造函数重载
  /// </summary>
  /// <param name="to">收件人地址</param>
  /// <param name="from">发件人地址</param>
  /// <param name="subject">邮件标题</param>
  /// <param name="body">邮件正文</param>
  /// <param name="host">发送邮件服务器名或地址</param>
  /// <param name="username">发件人用户名</param>
  /// <param name="password">发件人密码</param>
  public sendmail(string to, string from, string subject, string body, string host, string username, string password, string filename)
  {
   m_to = to;
   m_from = from;
   m_subject = subject;
   m_body = body;
   m_host = host;
   m_username = username;
   m_password = password;
   m_file = filename;
  }
  #endregion
  #region 数据属性
  //收件人地址
  public string to
  {
   get
   {
    return m_to;
   }
   set
   {
    m_to = value;
   }
  }
  //发件人地址
  public string from
  {
   get
   {
    return m_from;
   }
   set
   {
    m_from = value;
   }
  }
  //邮件标题
  public string subject
  {
   get
   {
    return m_subject;
   }
   set
   {
    m_subject = value;
   }
  }
  //邮件正文
  public string body
  {
   get
   {
    return m_body;
   }
   set
   {
    m_body = value;
   }
  }
  //服务器地址(名)
  public string host
  {
   get
   {
    return m_host;
   }
   set
   {
    m_host = value;
   }
  }
  //发件人的用户名
  public string username
  {
   get
   {
    return m_username;
   }
   set
   {
    m_username = value;
   }
  }
  //发件人的密码
  public string password
  {
   get
   {
    return m_password;
   }
   set
   {
    m_password = value;
   }
  }
  //邮件附件
  public string file
  {
   get
   {
    return m_file;
   }
   set
   {
    m_file = value;
   }
  }
  #endregion
  /// <summary>
  /// 发送邮件
  /// </summary>
  /// <returns>发送是否成功</returns>
  public bool send()
  {
   try
   {
    //获取所有的收件人地址
    char[] ch = { ',' };
    string[] address = m_to.split(ch);
    mailaddresscollection alladdress = new mailaddresscollection();
    for (int i = 0; i < address.length; i++)
    {
     //收件人地址
     mailaddress toaddress = new mailaddress(address[i]);
     alladdress.add(toaddress);
     //发件人地址
     mailaddress fromaddress = new mailaddress(m_from);
     //定义邮件消息
     mailmessage msg = new mailmessage(fromaddress, toaddress);
     //邮件标题
     msg.subject = m_subject;
     //邮件正文
     msg.body = m_body;
     //获取所有邮件附件
     char[] cr = { ';' };
     string[] file = m_file.split(cr);
     for (int n = 0; n < file.length; n++)
     {
      if (file[n] != "")
      {
       //附件对象
       attachment data = new attachment(file[n], mediatypenames.application.octet);
       //附件资料
       contentdisposition disposition = data.contentdisposition;
       disposition.creationdate = system.io.file.getcreationtime(file[n]);
       disposition.modificationdate = system.io.file.getlastwritetime(file[n]);
       disposition.readdate = system.io.file.getlastaccesstime(file[n]);
       //加入邮件附件
       msg.attachments.add(data);
      }
     }
     //使用简单邮件传输协议来传送邮件
     smtpclient sendmail = new smtpclient();
     //发送邮件的服务器名或地址
     sendmail.host = m_host;
     //验证发件人的身份
     sendmail.credentials = new networkcredential(m_username, m_password);
     //处理待发送邮件的方法
     sendmail.deliverymethod = smtpdeliverymethod.network;
     //发送邮件
     sendmail.send(msg);     
    }
    return true;
   }
   catch (exception ex)
   {
    return false;
   }
  }
 }
}

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