邮件发送代码

  1. using System;  
  2. using System.Net;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5.   
  6. namespace NET.Utility  
  7. {  
  8.     public class Mail  
  9.     {  
  10.         //邮件发送客户端  
  11.         private System.Net.Mail.SmtpClient client;  
  12.         //邮件发送的消息  
  13.         private System.Net.Mail.MailMessage _message;  
  14.  
  15.         #region 构造函数  
  16.         /// <summary>  
  17.         /// 构造自定义参数的Mail客户端  
  18.         /// </summary>  
  19.         /// <param name="host">Mail客户端Host</param>  
  20.         /// <param name="port">Mail客户端端口</param>  
  21.         public Mail(string host, int port)  
  22.         {  
  23.             this.client = new System.Net.Mail.SmtpClient(host, port);  
  24.             this._message = new System.Net.Mail.MailMessage();  
  25.         }  
  26.   
  27.         /// <summary>  
  28.         /// 构造host和port默认为smtp.qq.com和25的Mail客户端  
  29.         /// </summary>  
  30.         public Mail()  
  31.             : this("smtp.qq.com", 25)  
  32.         { }  
  33.         #endregion  
  34.  
  35.         #region 邮件部件,属性  
  36.         private string _from;  
  37.         /// <summary>  
  38.         /// 获取或设置邮件发送人地址   
  39.         /// </summary>  
  40.         public string From  
  41.         {  
  42.             get { return this._from; }  
  43.             set  
  44.             {  
  45.                 this._from = value;  
  46.                 this._message.From = new System.Net.Mail.MailAddress(value);  
  47.                 this._message.Sender = new System.Net.Mail.MailAddress(value);  
  48.             }  
  49.         }  
  50.   
  51.         private string _passWord;  
  52.         /// <summary>  
  53.         /// 获取或设置邮件发送人密码  
  54.         /// </summary>  
  55.         public string PassWord  
  56.         {  
  57.             get { return _passWord; }  
  58.             set { _passWord = value; }  
  59.         }  
  60.   
  61.         private List<string> _attachment;  
  62.         /// <summary>  
  63.         /// 获取或设置邮件附件地址列表  
  64.         /// </summary>  
  65.         public List<string> Attachment  
  66.         {  
  67.             get  
  68.             {  
  69.                 return _attachment;  
  70.             }  
  71.             set  
  72.             {  
  73.                 this._attachment = value;  
  74.                 StringBuilder sb = new StringBuilder();  
  75.                 foreach (string localPath in value)  
  76.                 {  
  77.                     if (System.IO.File.Exists(localPath))  
  78.                     {  
  79.                         this._message.Attachments.Add(new System.Net.Mail.Attachment(localPath));  
  80.                     }  
  81.                     else  
  82.                     {  
  83.                         sb.Append(localPath + "\r\n");  
  84.                     }  
  85.                 }  
  86.                 if (sb.Length != 0)  
  87.                     System.Windows.Forms.MessageBox.Show(sb.ToString() + "给定地址附件不存在,请检查");  
  88.             }  
  89.         }  
  90.   
  91.         private List<string> _bcc;  
  92.         /// <summary>  
  93.         /// 获取或设置邮件密送人地址列表  
  94.         /// </summary>  
  95.         public List<string> BCC  
  96.         {  
  97.             get { return _bcc; }  
  98.             set  
  99.             {  
  100.                 this._bcc = value;  
  101.                 StringBuilder sb = new StringBuilder();  
  102.                 foreach (string bcc in value)  
  103.                 {  
  104.                     if (InvalidMail.isEmail(bcc))  
  105.                     {  
  106.                         this._message.Bcc.Add(bcc);  
  107.                     }  
  108.                     else  
  109.                     {  
  110.                         sb.Append(bcc + "\r\n");  
  111.                     }  
  112.                 }  
  113.                 if (sb.Length != 0)  
  114.                     System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");  
  115.             }  
  116.         }  
  117.   
  118.         private List<string> _cc;  
  119.         /// <summary>  
  120.         /// 获取或设置邮件抄送人地址列表  
  121.         /// </summary>  
  122.         public List<string> CC  
  123.         {  
  124.             get { return _cc; }  
  125.             set  
  126.             {  
  127.                 _cc = value;  
  128.                 StringBuilder sb = new StringBuilder();  
  129.                 foreach (string cc in value)  
  130.                 {  
  131.                     if (InvalidMail.isEmail(cc))  
  132.                     {  
  133.                         this._message.CC.Add(cc);  
  134.                     }  
  135.                     else  
  136.                     {  
  137.                         sb.Append(cc + "\r\n");  
  138.                     }  
  139.                 }  
  140.                 if (sb.Length != 0)  
  141.                     System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");  
  142.             }  
  143.         }  
  144.   
  145.         /// <summary>  
  146.         /// 获取或设置邮件正文  
  147.         /// </summary>  
  148.         public string Body  
  149.         {  
  150.             get { return this._message.Body; }  
  151.             set { this._message.Body = value; }  
  152.         }  
  153.   
  154.         /// <summary>  
  155.         /// 获取或设置邮件正文是否Html格式  
  156.         /// </summary>  
  157.         public bool IsHtml  
  158.         {  
  159.             get { return this._message.IsBodyHtml; }  
  160.             set { this._message.IsBodyHtml = value; }  
  161.         }  
  162.   
  163.         /// <summary>  
  164.         /// 获取或设置邮件正文的编码格式  
  165.         /// </summary>  
  166.         public Encoding BodyEncoding  
  167.         {  
  168.             get { return this._message.BodyEncoding; }  
  169.             set { this._message.BodyEncoding = value; }  
  170.         }  
  171.   
  172.         /// <summary>  
  173.         /// 获取或设置获取或设置邮件主题  
  174.         /// </summary>  
  175.         public string Subject  
  176.         {  
  177.             get { return this._message.Subject; }  
  178.             set { this._message.Subject = value; }  
  179.         }  
  180.   
  181.         /// <summary>  
  182.         /// 获取或设置邮件主题编码  
  183.         /// </summary>  
  184.         public Encoding SubjectEncoding  
  185.         {  
  186.             get { return this._message.SubjectEncoding; }  
  187.             set { this._message.SubjectEncoding = value; }  
  188.         }  
  189.   
  190.         private List<string> _to;  
  191.         /// <summary>  
  192.         /// 获取或设置收件人列表  
  193.         /// </summary>  
  194.         public List<string> To  
  195.         {  
  196.             get  
  197.             {  
  198.                 return _to;  
  199.             }  
  200.             set  
  201.             {  
  202.                 _to = value;  
  203.                 StringBuilder sb = new StringBuilder();  
  204.                 foreach (string to in value)  
  205.                 {  
  206.                     if (InvalidMail.isEmail(to))  
  207.                     {  
  208.                         this._message.To.Add(to);  
  209.                     }  
  210.                     else  
  211.                     {  
  212.                         sb.Append(to + "\r\n");  
  213.                     }  
  214.                 }  
  215.                 if (sb.Length != 0)  
  216.                     System.Windows.Forms.MessageBox.Show(sb.ToString() + "邮件地址格式不正确,请检查");  
  217.             }  
  218.         }  
  219.         #endregion  
  220.  
  221.         #region 发送邮件  
  222.   
  223.         /// <summary>  
  224.         /// 检查邮件必要信息是否存在  
  225.         /// </summary>  
  226.         /// <returns></returns>  
  227.         private bool CheckMail()  
  228.         {  
  229.             bool flag = true;  
  230.             if (this.Body == null)  
  231.             {  
  232.                 flag = false;  
  233.                 System.Windows.Forms.MessageBox.Show("邮件正文不能为空");  
  234.             }  
  235.             if (this.From == null)  
  236.             {  
  237.                 flag = false;  
  238.                 System.Windows.Forms.MessageBox.Show("邮件发送人不能为空");  
  239.             }  
  240.             if (this.PassWord == null)  
  241.             {  
  242.                 flag = false;  
  243.                 System.Windows.Forms.MessageBox.Show("邮件发送人密码不能为空");  
  244.             }  
  245.             if (this.To == null)  
  246.             {  
  247.                 flag = false;  
  248.                 System.Windows.Forms.MessageBox.Show("邮件收件人不能为空");  
  249.             }  
  250.             if (this.Subject == null)  
  251.             {  
  252.                 flag = false;  
  253.                 System.Windows.Forms.MessageBox.Show("邮件主题不能为空");  
  254.             }  
  255.             return flag;  
  256.         }  
  257.   
  258.         /// <summary>  
  259.         /// 同步发送邮件  
  260.         /// </summary>  
  261.         /// <returns></returns>  
  262.         public bool SendMail()  
  263.         {  
  264.             bool flag = false;  
  265.             if (CheckMail())  
  266.             {  
  267.                 try  
  268.                 {  
  269.                     this.client.Credentials = new System.Net.NetworkCredential(_from, PassWord);  
  270.                     this.client.Send(this._message);  
  271.                     flag = true;  
  272.                 }  
  273.                 catch (System.InvalidOperationException e)  
  274.                 {  
  275.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  276.                 }  
  277.                 catch (System.ArgumentNullException e)  
  278.                 {  
  279.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  280.                 }  
  281.                 catch (System.ArgumentOutOfRangeException e)  
  282.                 {  
  283.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  284.                 }  
  285.                 catch (System.Net.Mail.SmtpException e)  
  286.                 {  
  287.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  288.                 }  
  289.                 catch (Exception e)  
  290.                 {  
  291.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  292.                 }  
  293.             }  
  294.             return flag;  
  295.         }  
  296.   
  297.         /// <summary>  
  298.         /// 异步发送邮件  
  299.         /// </summary>  
  300.         public void SendMailAsync()  
  301.         {  
  302.             if (CheckMail())  
  303.             {  
  304.                 try  
  305.                 {  
  306.                     this.client.Credentials = new System.Net.NetworkCredential(_from, PassWord);  
  307.                     this.client.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(client_SendCompleted);  
  308.                     object flag = this._message;  
  309.                     this.client.SendAsync(this._message, flag);  
  310.                 }  
  311.                 catch (System.InvalidOperationException e)  
  312.                 {  
  313.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  314.                 }  
  315.                 catch (System.ArgumentNullException e)  
  316.                 {  
  317.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  318.                 }  
  319.                 catch (System.ArgumentOutOfRangeException e)  
  320.                 {  
  321.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  322.                 }  
  323.                 catch (System.Net.Mail.SmtpException e)  
  324.                 {  
  325.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  326.                 }  
  327.                 catch (Exception e)  
  328.                 {  
  329.                     System.Windows.Forms.MessageBox.Show(e.Message);  
  330.                 }  
  331.             }  
  332.         }  
  333.   
  334.         /// <summary>  
  335.         /// 异步发送邮件完成时事件  
  336.         /// </summary>  
  337.         /// <param name="sender"></param>  
  338.         /// <param name="e"></param>  
  339.         void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)  
  340.         {  
  341.             System.Net.Mail.MailMessage message = (System.Net.Mail.MailMessage)e.UserState;  
  342.             if (e.Cancelled)  
  343.             {  
  344.                 if (SendComplet != null)  
  345.                 {  
  346.                     SendComplet(thisnew SendCompletEventArgs(DateTime.Now, false));  
  347.                 }  
  348.             }  
  349.             else if (e.Error != null)  
  350.             {  
  351.                 if (SendComplet != null)  
  352.                 {  
  353.                     SendComplet(thisnew SendCompletEventArgs(DateTime.Now, false));  
  354.                 }  
  355.             }  
  356.             else  
  357.             {  
  358.                 if (SendComplet != null)  
  359.                 {  
  360.                     SendComplet(thisnew SendCompletEventArgs(DateTime.Now, true));  
  361.                 }  
  362.             }  
  363.         }  
  364.         public event SendCompletEventHandler SendComplet;  
  365.         #endregion  
  366.     }  
  367.   
  368.     /// <summary>  
  369.     /// 邮件异步发送完成时发生事件的委托  
  370.     /// </summary>  
  371.     /// <param name="sender"></param>  
  372.     /// <param name="e"></param>  
  373.     public delegate void SendCompletEventHandler(object sender,EventArgs e);  
  374.   
  375.     /// <summary>  
  376.     /// 自定义参数  
  377.     /// </summary>  
  378.     public class SendCompletEventArgs : EventArgs  
  379.     {  
  380.         public readonly DateTime dateTime;  
  381.         public readonly bool success;  
  382.         public SendCompletEventArgs(DateTime dateTime, bool success)  
  383.         {  
  384.             this.dateTime = dateTime;  
  385.             this.success = success;  
  386.         }  
  387.     }  
  388.   
  389.     /// <summary>  
  390.     /// 静态验证类  
  391.     /// </summary>  
  392.     public static class InvalidMail  
  393.     {  
  394.         /// <summary>  
  395.         /// 判断用户输入的字符串是否符合邮件地址格式  
  396.         /// </summary>  
  397.         /// <param name="email"></param>  
  398.         /// <returns></returns>  
  399.         public static bool isEmail(string email)  
  400.         {  
  401.             string valid = @"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+{1}quot;;  
  402.             return System.Text.RegularExpressions.Regex.IsMatch(email, valid);  
  403.         }  
  404.     }  


客户端调用邮件发送

  1. /// <summary>  
  2. /// 发送邮件  
  3. /// </summary>  
  4. private void SendMail()  
  5. {  
  6.     Mail mail = new Mail();  
  7.   
  8.     //设置邮件附件  
  9.     //非必要  
  10.     List<string> attachment=new List<string> ();  
  11.     OpenFileDialog ofd = new OpenFileDialog();  
  12.     if (ofd.ShowDialog() == DialogResult.OK)  
  13.     {  
  14.         foreach (string path in ofd.FileNames)  
  15.         {  
  16.             attachment.Add(path);  
  17.         }  
  18.     }  
  19.     mail.Attachment = attachment;  
  20.   
  21.     //设置邮件密送人列表  
  22.     //非必要  
  23.     List<string> bcc = new List<string>();  
  24.     bcc.Add("xxxx@qq.com");  
  25.     bcc.Add("yyyy@qq.com");  
  26.     mail.BCC = bcc;  
  27.   
  28.     //设置抄送人列表  
  29.     //非必要  
  30.     List<string> cc = new List<string>();  
  31.     cc.Add("aaaa.qq.com");  
  32.     cc.Add("bbbb.qq.com");  
  33.     mail.CC = cc;  
  34.   
  35.     //设置邮件正文  
  36.     //非必要  
  37.     mail.Body = "Microsoft Visual Studio<br/>C#.Net";  
  38.   
  39.     //设置邮件正文编码格式  
  40.     //非必要  
  41.     mail.BodyEncoding = Encoding.UTF8;  
  42.   
  43.     //设置邮件发送人邮箱地址  
  44.     //必要  
  45.     mail.From = "userName@qq.com";  
  46.   
  47.     //设置邮件发送人邮箱密码  
  48.     //非必要  
  49.     mail.PassWord = "passWord";  
  50.     //邮件正文是否以html格式在邮件中显示  
  51.     //如果为false,则收件人收到的邮件正文为:Microsoft Visual Studio<br/>C#.Net  
  52.     //如果为true,则收件人收到的邮件正文如下:  
  53.     //Microsoft Visual Studio  
  54.     //C#.Net  
  55.     //非必要  
  56.     mail.IsHtml = true;  
  57.   
  58.     //设置邮件标题  
  59.     //非必要  
  60.     mail.Subject = "try";  
  61.   
  62.     //设置邮件收件人  
  63.     //必要  
  64.     List<string> to = new List<string>();  
  65.     to.Add("qqqq@qq.com");  
  66.     to.Add("wwww@qq.com");  
  67.     to.Add("rrrr@qq.com");  
  68.     mail.To = to;  
  69.   
  70.     //发送邮件,使用该方法在发送邮件时,线程会阻断,直到邮件发送完成  
  71.     if (mail.SendMail())  
  72.     {  
  73.         Console.WriteLine("do something");  
  74.     }  
  75.   
  76.     //增加异步发送邮件事件  
  77.     mail.SendComplet+=new SendCompletEventHandler(mail_SendComplet);  
  78.   
  79.     //异步发送邮件,使用该方法发送邮件时,线程不会被阻断  
  80.     //邮件发送完成后,会引发SendComplet事件  
  81.     mail.SendMailAsync();  
  82. }  
  83.   
  84. /// <summary>  
  85. /// 异步邮件发送完成后  
  86. /// </summary>  
  87. /// <param name="sender"></param>  
  88. /// <param name="e"></param>  
  89. void mail_SendComplet(object sender, SendCompletEventArgs e)  
  90. {  
  91.     Console.WriteLine("do something");  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值