Mail C#

来源:互联网 发布:jdk 7u67 windows x32 编辑:程序博客网 时间:2024/06/06 09:18
  1. using System;
  2. using System.Net;
  3. using System.Net.Mail;
  4. using System.Text;
  5. using System.ComponentModel;
  6. namespace Tmiqpl.Net
  7. {
  8.     /// <summary>
  9.     /// 简单邮件传输协议
  10.     /// author: Tmiqpl
  11.     /// </summary>
  12.     public class SMTP : IDisposable
  13.     {
  14.         #region 预定义
  15.         private string server = null;
  16.         private string username = null;
  17.         private string password = null;
  18.         private int port = 25;
  19.         private bool network = true;
  20.         private bool ishtml = true;
  21.         private string subject = null;
  22.         private string body = null;
  23.         private string from = null;
  24.         private string to = null;
  25.         private string errortext = null;
  26.         private string[] attachement = null;
  27.         private Encoding bodyencoding = Encoding.Default;
  28.         private Encoding subjectencoding = Encoding.Default;
  29.         private Component conponent = new Component();
  30.         private bool disposed = false;
  31.         private string[] cc = null;
  32.         private string[] bcc = null;
  33.         #endregion
  34.         /// <summary>
  35.         /// 初始化邮件传输协议
  36.         /// </summary>
  37.         /// <param name="servername">邮件服务器地址</param>
  38.         public SMTP(string servername)
  39.             : this(servername, 25)
  40.         {
  41.         }
  42.         /// <summary>
  43.         /// 初始化邮件传输协议
  44.         /// </summary>
  45.         /// <param name="servername">邮件服务器地址</param>
  46.         /// <param name="prot">端口</param>
  47.         public SMTP(string servername, int prot)
  48.             : this(servername, prot, nullnull)
  49.         {
  50.         }
  51.         /// <summary>
  52.         /// 初始化邮件传输协议
  53.         /// </summary>
  54.         /// <param name="servername">邮件服务器地址</param>
  55.         /// <param name="prot">端口</param>
  56.         /// <param name="username">用户名</param>
  57.         /// <param name="password">密码</param>
  58.         public SMTP(string servername, int prot, string username, string password)
  59.         {
  60.             this.ServerName = servername;
  61.             this.port = prot;
  62.             this.UserName = username;
  63.             this.PassWord = password;
  64.         }
  65.         ~SMTP()
  66.         {
  67.             this.Dispose(false);
  68.         }
  69.         protected void Dispose(bool disposed)
  70.         {
  71.             if (!this.disposed && disposed)
  72.             {
  73.                 conponent.Dispose();
  74.             }
  75.             this.disposed = true;
  76.         }
  77.         /// <summary>
  78.         /// 发送
  79.         /// </summary>
  80.         public bool Send()
  81.         {
  82.             SmtpClient _client;
  83.             if (this.ServerName == null)
  84.             {
  85.                 this.errortext = "(SMTP) 服务器未定义";
  86.                 return false;
  87.             }
  88.             _client = new SmtpClient(this.server, this.port);
  89.             _client.UseDefaultCredentials = false;
  90.             if ((this.username == null) || (this.password == null))
  91.             {
  92.                 _client.Credentials = CredentialCache.DefaultNetworkCredentials;
  93.             }
  94.             else
  95.             {
  96.                 _client.Credentials = new NetworkCredential(this.username, this.password);
  97.             }
  98.             if (this.network)
  99.             {
  100.                 _client.DeliveryMethod = SmtpDeliveryMethod.Network;
  101.             }
  102.             else
  103.             {
  104.                 _client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
  105.             }
  106.             if (this.from == null)
  107.             {
  108.                 this.errortext = "发件人地址未定义!";
  109.                 return false;
  110.             }
  111.             if (this.to == null)
  112.             {
  113.                 this.errortext = "收件人人地址未定义!";
  114.                 return false;
  115.             }
  116.             MailAddress from = new MailAddress(this.from);
  117.             MailAddress to = new MailAddress(this.to);
  118.             MailMessage msg = new MailMessage(from, to);
  119.             if (this.subject == null)
  120.             {
  121.                 this.errortext = "邮件主题未定义!";
  122.                 return false;
  123.             }
  124.             msg.Subject = this.subject;
  125.             msg.Body = this.body;
  126.             if (this.cc != null)
  127.             {
  128.                 if (this.cc.Length > 0)
  129.                 {
  130.                     foreach (string cc in this.cc)
  131.                     {
  132.                         msg.CC.Add(cc);
  133.                     }
  134.                 }
  135.             }
  136.             if (this.bcc != null)
  137.             {
  138.                 if (this.bcc.Length > 0)
  139.                 {
  140.                     foreach (string bcc in this.bcc)
  141.                     {
  142.                         msg.Bcc.Add(bcc);
  143.                     }
  144.                 }
  145.             }
  146.             if (this.attachement != null)
  147.             {
  148.                 if (this.attachement.Length > 0)
  149.                 {
  150.                     foreach (string att in this.attachement)
  151.                     {
  152.                         msg.Attachments.Add(new Attachment(att));
  153.                     }
  154.                 }
  155.             }
  156.             msg.BodyEncoding = this.bodyencoding;
  157.             msg.SubjectEncoding = this.subjectencoding;
  158.             msg.IsBodyHtml = this.ishtml;
  159.             try
  160.             {
  161.                 _client.Send(msg);
  162.                 return true;
  163.             }
  164.             catch (Exception e)
  165.             {
  166.                 this.errortext = e.Message;
  167.                 return false;
  168.             }
  169.             finally
  170.             {
  171.                 msg.Dispose();
  172.             }
  173.         }
  174.         #region 属性
  175.         /// <summary>
  176.         /// 获取或设置 SMTP 服务器地址
  177.         /// </summary>
  178.         public string ServerName
  179.         {
  180.             get { return this.server; }
  181.             set { this.server = value; }
  182.         }
  183.         /// <summary>
  184.         /// 获取或设置登陆 SMTP 服务器的用户名
  185.         /// </summary>
  186.         public string UserName
  187.         {
  188.             get { return this.username; }
  189.             set { this.username = value; }
  190.         }
  191.         /// <summary>
  192.         /// 获取或设置登陆 SMTP 服务器的密码
  193.         /// </summary>
  194.         public string PassWord
  195.         {
  196.             get { return this.password; }
  197.             set { this.password = value; }
  198.         }
  199.         /// <summary>
  200.         /// 获取或设置 SMTP 端口
  201.         /// </summary>
  202.         public int Port
  203.         {
  204.             get { return this.port; }
  205.             set { this.port = value; }
  206.         }
  207.         /// <summary>
  208.         /// 获取或设置是否通过网络 SMTP 服务器发送
  209.         /// </summary>
  210.         public bool IsNetwork
  211.         {
  212.             get { return this.network; }
  213.             set { this.network = value; }
  214.         }
  215.         /// <summary>
  216.         /// 获取或设置邮件内容是否以 HTML 格式发送
  217.         /// </summary>
  218.         public bool IsHtml
  219.         {
  220.             get { return this.ishtml; }
  221.             set { this.ishtml = value; }
  222.         }
  223.         /// <summary>
  224.         /// 获取或设置邮件内容
  225.         /// </summary>
  226.         public string Body
  227.         {
  228.             get { return this.body; }
  229.             set { this.body = value; }
  230.         }
  231.         /// <summary>
  232.         /// 获取或设置邮件主题行
  233.         /// </summary>
  234.         public string Subject
  235.         {
  236.             get { return this.subject; }
  237.             set { this.subject = value; }
  238.         }
  239.         /// <summary>
  240.         /// 获取或设置邮件发送人
  241.         /// </summary>
  242.         public string From
  243.         {
  244.             get { return this.from; }
  245.             set { this.from = value; }
  246.         }
  247.         /// <summary>
  248.         /// 获取或设置邮件接收人
  249.         /// </summary>
  250.         public string To
  251.         {
  252.             get { return this.to; }
  253.             set { this.to = value; }
  254.         }
  255.         /// <summary>
  256.         /// 获取邮件发送失败后的信息描述
  257.         /// </summary>
  258.         public string ErrorText
  259.         {
  260.             get { return this.errortext; }
  261.         }
  262.         /// <summary>
  263.         /// 获取或设置邮件附件
  264.         /// </summary>
  265.         public string[] Attachement
  266.         {
  267.             get { return this.attachement; }
  268.             set { this.attachement = value; }
  269.         }
  270.         /// <summary>
  271.         /// 获取或设置邮件抄送地址
  272.         /// </summary>
  273.         public string[] Cc
  274.         {
  275.             get { return this.cc; }
  276.             set { this.cc = value; }
  277.         }
  278.         /// <summary>
  279.         /// 获取或设置邮件密送地址
  280.         /// </summary>
  281.         public string[] Bcc
  282.         {
  283.             get { return this.bcc; }
  284.             set { this.bcc = value; }
  285.         }
  286.         /// <summary>
  287.         /// 获取或设置邮件内容编码格式
  288.         /// </summary>
  289.         public Encoding BodyEncoding
  290.         {
  291.             get { return this.bodyencoding; }
  292.             set { this.bodyencoding = value; }
  293.         }
  294.         /// <summary>
  295.         /// 获取或设置邮件主题编码格式
  296.         /// </summary>
  297.         public Encoding SubjectEncoding
  298.         {
  299.             get { return this.subjectencoding; }
  300.             set { this.subjectencoding = value; }
  301.         }
  302.         #endregion
  303.         /// <summary>
  304.         /// 释放该对象非托管资源
  305.         /// </summary>
  306.         public virtual void Dispose()
  307.         {
  308.             this.Dispose(true);
  309.             GC.SuppressFinalize(this);
  310.         }
  311.     }
  312. }
原创粉丝点击