欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2022-02-15 11:08:26|栏目:.NET代码|点击:

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

上一篇:适用于WebForm Mvc的Pager分页组件C#实现

栏    目:.NET代码

下一篇:Unity Shader实现黑幕过场效果

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

本文地址:http://www.codeinn.net/misctech/193478.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有