用.net发送 Email 帮助类 EmailHelper

refenrence:

http://blog.csdn.net/yenange/article/details/19981169




1. 配置文件 App.config

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <appSettings>  
  4.     <!-- Smtp 服务器 -->  
  5.     <add key="SmtpHost" value="smtp.qq.com" />  
  6.     <!-- Smtp 服务器端口 -->  
  7.     <add key="SmtpPort" value="25" />  
  8.     <!--发送者 Email 地址-->  
  9.     <add key="FromEmailAddress" value="xx@qq.com" />  
  10.     <!--发送者 Email 密码-->  
  11.     <add key="FormEmailPassword" value="??????" />  
  12.   </appSettings>  
  13. </configuration>  

2. EmailHelper 类

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Mail;  
  7. using System.Text;  
  8.   
  9. namespace ConsoleApplication1  
  10. {  
  11.     /// <summary>  
  12.     /// Author      : yenange  
  13.     /// Date        : 2014-02-26  
  14.     /// Description : 邮件发送辅助类  
  15.     /// </summary>  
  16.     public class EmailHelper  
  17.     {  
  18.         #region [ 属性(发送Email相关) ]  
  19.         private string _SmtpHost = string.Empty;  
  20.         private int _SmtpPort = -1;  
  21.         private string _FromEmailAddress = string.Empty;  
  22.         private string _FormEmailPassword = string.Empty;  
  23.   
  24.         /// <summary>  
  25.         /// smtp 服务器   
  26.         /// </summary>  
  27.         public string SmtpHost  
  28.         {  
  29.             get  
  30.             {  
  31.                 if (string.IsNullOrEmpty(_SmtpHost))  
  32.                 {  
  33.                     _SmtpHost = ConfigurationManager.AppSettings["SmtpHost"];  
  34.                 }  
  35.                 return _SmtpHost;  
  36.             }  
  37.         }  
  38.         /// <summary>  
  39.         /// smtp 服务器端口  默认为25  
  40.         /// </summary>  
  41.         public int SmtpPort  
  42.         {  
  43.             get  
  44.             {  
  45.                 if (_SmtpPort == -1)  
  46.                 {  
  47.                     if (!int.TryParse(ConfigurationManager.AppSettings["SmtpPort"], out _SmtpPort))  
  48.                     {  
  49.                         _SmtpPort = 25;  
  50.                     }  
  51.                 }  
  52.                 return _SmtpPort;  
  53.             }  
  54.         }  
  55.         /// <summary>  
  56.         /// 发送者 Eamil 地址  
  57.         /// </summary>  
  58.         public string FromEmailAddress  
  59.         {  
  60.             get  
  61.             {  
  62.                 if (string.IsNullOrEmpty(_FromEmailAddress))  
  63.                 {  
  64.                     _FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"];  
  65.                 }  
  66.                 return _FromEmailAddress;  
  67.             }  
  68.         }  
  69.   
  70.         /// <summary>  
  71.         /// 发送者 Eamil 密码  
  72.         /// </summary>  
  73.         public string FormEmailPassword  
  74.         {  
  75.             get  
  76.             {  
  77.                 if (string.IsNullOrEmpty(_FormEmailPassword))  
  78.                 {  
  79.                     _FormEmailPassword = ConfigurationManager.AppSettings["FormEmailPassword"];  
  80.                 }  
  81.                 return _FormEmailPassword;  
  82.             }  
  83.         }  
  84.         #endregion  
  85.  
  86.         #region [ 属性(邮件相关) ]  
  87.         /// <summary>  
  88.         /// 收件人 Email 列表,多个邮件地址之间用 半角逗号 分开  
  89.         /// </summary>  
  90.         public string ToList { getset; }  
  91.         /// <summary>  
  92.         /// 邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号 分开  
  93.         /// </summary>  
  94.         public string CCList { getset; }  
  95.         /// <summary>  
  96.         /// 邮件的密送者,支持群发,多个邮件地址之间用 半角逗号 分开  
  97.         /// </summary>  
  98.         public string BccList { getset; }  
  99.         /// <summary>  
  100.         /// 邮件标题  
  101.         /// </summary>  
  102.         public string Subject { getset; }  
  103.         /// <summary>  
  104.         /// 邮件正文  
  105.         /// </summary>  
  106.         public string Body { getset; }  
  107.   
  108.         private bool _IsBodyHtml = true;  
  109.         /// <summary>  
  110.         /// 邮件正文是否为Html格式  
  111.         /// </summary>  
  112.         public bool IsBodyHtml  
  113.         {  
  114.             get { return _IsBodyHtml; }  
  115.             set { _IsBodyHtml = value; }  
  116.         }  
  117.         /// <summary>  
  118.         /// 附件列表  
  119.         /// </summary>  
  120.         public List<Attachment> AttachmentList { getset; }  
  121.         #endregion  
  122.  
  123.         #region [ 构造函数 ]  
  124.         /// <summary>  
  125.         /// 构造函数 (body默认为html格式)  
  126.         /// </summary>  
  127.         /// <param name="toList">收件人列表</param>  
  128.         /// <param name="subject">邮件标题</param>  
  129.         /// <param name="body">邮件正文</param>  
  130.         public EmailHelper(string toList, string subject, string body)  
  131.         {  
  132.             this.ToList = toList;  
  133.             this.Subject = subject;  
  134.             this.Body = body;  
  135.         }  
  136.         /// <summary>  
  137.         /// 构造函数  
  138.         /// </summary>  
  139.         /// <param name="toList">收件人列表</param>  
  140.         /// <param name="subject">邮件标题</param>  
  141.         /// <param name="isBodyHtml">邮件正文是否为Html格式</param>  
  142.         /// <param name="body">邮件正文</param>  
  143.         public EmailHelper(string toList, string subject, bool isBodyHtml, string body)  
  144.         {  
  145.             this.ToList = toList;  
  146.             this.Subject = subject;  
  147.             this.IsBodyHtml = isBodyHtml;  
  148.             this.Body = body;  
  149.         }  
  150.         /// <summary>  
  151.         /// 构造函数  
  152.         /// </summary>  
  153.         /// <param name="toList">收件人列表</param>  
  154.         /// <param name="ccList">抄送人列表</param>  
  155.         /// <param name="bccList">密送人列表</param>  
  156.         /// <param name="subject">邮件标题</param>  
  157.         /// <param name="isBodyHtml">邮件正文是否为Html格式</param>  
  158.         /// <param name="body">邮件正文</param>  
  159.         public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body)  
  160.         {  
  161.             this.ToList = toList;  
  162.             this.CCList = ccList;  
  163.             this.BccList = bccList;  
  164.             this.Subject = subject;  
  165.             this.IsBodyHtml = isBodyHtml;  
  166.             this.Body = body;  
  167.         }  
  168.         /// <summary>  
  169.         /// 构造函数  
  170.         /// </summary>  
  171.         /// <param name="toList">收件人列表</param>  
  172.         /// <param name="ccList">抄送人列表</param>  
  173.         /// <param name="bccList">密送人列表</param>  
  174.         /// <param name="subject">邮件标题</param>  
  175.         /// <param name="isBodyHtml">邮件正文是否为Html格式</param>  
  176.         /// <param name="body">邮件正文</param>  
  177.         /// <param name="attachmentList">附件列表</param>  
  178.         public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body, List<Attachment> attachmentList)  
  179.         {  
  180.             this.ToList = toList;  
  181.             this.CCList = ccList;  
  182.             this.BccList = bccList;  
  183.             this.Subject = subject;  
  184.             this.IsBodyHtml = isBodyHtml;  
  185.             this.Body = body;  
  186.             this.AttachmentList = attachmentList;  
  187.         }  
  188.         #endregion  
  189.  
  190.         #region [ 发送邮件 ]  
  191.         /// <summary>  
  192.         /// 发送邮件  
  193.         /// </summary>  
  194.         /// <returns></returns>  
  195.         public void Send()  
  196.         {  
  197.             SmtpClient smtp = new SmtpClient();                 //实例化一个SmtpClient  
  198.             smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //将smtp的出站方式设为 Network  
  199.             smtp.EnableSsl = false;                             //smtp服务器是否启用SSL加密  
  200.             smtp.Host = this.SmtpHost;                          //指定 smtp 服务器地址  
  201.             smtp.Port = this.SmtpPort;                          //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去  
  202.             smtp.UseDefaultCredentials = true;                  //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了  
  203.             smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword);    //如果需要认证,则用下面的方式  
  204.   
  205.             MailMessage mm = new MailMessage(); //实例化一个邮件类  
  206.             mm.Priority = MailPriority.Normal; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可  
  207.             mm.From = new MailAddress(this.FromEmailAddress, "管理员", Encoding.GetEncoding(936));  
  208.   
  209.             //收件人  
  210.             if (!string.IsNullOrEmpty(this.ToList))  
  211.                 mm.To.Add(this.ToList);  
  212.             //抄送人  
  213.             if (!string.IsNullOrEmpty(this.CCList))  
  214.                 mm.CC.Add(this.CCList);  
  215.             //密送人  
  216.             if (!string.IsNullOrEmpty(this.BccList))  
  217.                 mm.Bcc.Add(this.BccList);  
  218.   
  219.             mm.Subject = this.Subject;                      //邮件标题  
  220.             mm.SubjectEncoding = Encoding.GetEncoding(936); //这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。  
  221.             mm.IsBodyHtml = this.IsBodyHtml;                //邮件正文是否是HTML格式  
  222.             mm.BodyEncoding = Encoding.GetEncoding(936);    //邮件正文的编码, 设置不正确, 接收者会收到乱码  
  223.             mm.Body = this.Body;                            //邮件正文  
  224.             //邮件附件  
  225.             if (this.AttachmentList != null && this.AttachmentList.Count > 0)  
  226.             {  
  227.                 foreach (Attachment attachment in this.AttachmentList)  
  228.                 {  
  229.                     mm.Attachments.Add(attachment);  
  230.                 }  
  231.             }  
  232.             //发送邮件,如果不返回异常, 则大功告成了。  
  233.             smtp.Send(mm);  
  234.         }  
  235.         #endregion  
  236.     }  
  237. }  



3. 测试代码:

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.Net.Mail;  
  7. using System.Configuration;  
  8.   
  9. namespace ConsoleApplication1  
  10. {  
  11.     class Program  
  12.     {  
  13.         static void Main(string[] args)  
  14.         {  
  15.             try   
  16.             {             
  17.                 EmailHelper email = new EmailHelper("xxx@qq.com,xxx@qq.com""测试邮件2""<html><body><div style='color:red;'>测试内容</div></body></html>");  
  18.                 email.Send();  
  19.                 Console.WriteLine("发送成功!");  
  20.             }  
  21.             catch (Exception ex)  
  22.             {  
  23.                 Console.WriteLine("发送失败!失败原因:");  
  24.                 Console.WriteLine(ex.Message);  
  25.             }  
  26.             Console.Read();  
  27.         }  
  28.     }//end of class Program  
  29. }//end of namespace  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值