亲宝软件园·资讯

展开

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

Autumoon 人气:0
想了解C#实现的自定义邮件发送类完整实例(支持多人多附件)的相关内容吗,Autumoon在本文为您仔细讲解C#自定义邮件发送类的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#,自定义,邮件发送类,C#邮件发送,下面大家一起来学习吧。

本文实例讲述了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#程序设计有所帮助。

加载全部内容

相关教程
猜你喜欢
用户评论