你的位置:首页 > 软件开发 > ASP.net > Smtp邮件发送系统公用代码整理—总结

Smtp邮件发送系统公用代码整理—总结

发布时间:2015-08-18 14:00:11
1.前言  a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等。那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一下:总结不全,纯属于整理封装的类。    (1):用户注册(推送邮件,提示用户 ...

1.前言

  a.在软件开发中,我们经常能够遇到给用户或者客户推送邮件,推送邮件也分为很多方式,比如:推送一句话,推送一个网页等等。那么在系统开发中我们一般在什么情况下会使用邮件发送呢?下面我简单总结了一下:总结不全,纯属于整理封装的类。

    (1):用户注册(推送邮件,提示用户注册成功,牢记用户名密码)

    (2):修改密码(推送邮件,提示用户密码修改成功)

    (3):下订单(推送邮件,提示用户订单已下)

    (4):物流跟踪(推送邮件,跟踪物流信息)

    (5):广告推送(推送广告,提示用户近来公司近况或者新的商品,产品)

    (6):日志监控系统推送(推送错误信息,提供给程序员使用)

    ................................................

  b.上面我们说了邮件推送的使用,那么既然邮件推送这么频繁,就需要整理出来一个公用类来给大家使用,下面我就简单整理了一下公用类,如果大家需要,请git上下载或者复制即可,不能保证百分百实现你的功能,如果不能实现你的功能,请改进。

  c.git下载地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/Smtp%E9%82%AE%E4%BB%B6%E5%8F%91%E9%80%81

  d.代码整理如下,请您查看.......

2.代码整理

  a.C#代码以及如何调用等都在代码中已注释,如不会使用,请仔细查看

 1 // 源文件头信息: 2 // <copyright file="Email.cs,EmailHelp.cs"> 3 // Copyright(c)2014-2034 Kencery.All rights reserved. 4 // 个人博客:http://www.cnblogs.com/hanyinglong 5 // 创建人:韩迎龙(kencery) 6 // 创建时间:2015-8-18 7 // </copyright> 8  9 using System; 10 using System.Net; 11 using System.Net.Mail; 12 using System.Text; 13  14 namespace KenceryCommonMethod 15 { 16   /// <summary> 17   /// 功能:实现邮件发送,分装发送邮件的调用方法 18   /// </summary> 19   /// <auther> 20   ///   <name>Kencery</name> 21   ///   <date>2015-8-18</date> 22   /// </auther> 23   public class Email 24   { 25     #region --------------------字段-------------------- 26  27     private get='_blank'>string _serviceType = "SMTP"; 28     private string _host; 29  30     #endregion 31  32     #region --------------------属性-------------------- 33  34     /// <summary> 35     /// 发送者邮箱 36     /// </summary> 37     public string From { get; set; } 38  39     /// <summary> 40     /// 接收者邮箱列表 41     /// </summary> 42     public string[] To { get; set; } 43  44     /// <summary> 45     /// 抄送者邮箱列表 46     /// </summary> 47     public string[] Cc { get; set; } 48  49     /// <summary> 50     /// 秘抄者邮箱列表 51     /// </summary> 52     public string[] Bcc { get; set; } 53  54     /// <summary> 55     /// 邮件主题 56     /// </summary> 57     public string Subject { get; set; } 58  59     /// <summary> 60     /// 邮件内容 61     /// </summary> 62     public string Body { get; set; } 63  64     /// <summary> 65     /// 是否是HTML格式 66     /// </summary> 67     public bool IsBodyHtml { get; set; } 68  69     /// <summary> 70     /// 附件列表 71     /// </summary> 72     public string[] Attachments { get; set; } 73  74     /// <summary> 75     /// 邮箱服务类型(Pop3,SMTP,IMAP,MAIL等),默认为SMTP 76     /// </summary> 77     public string ServiceType 78     { 79       get { return _serviceType; } 80       set { _serviceType = value; } 81     } 82  83     /// <summary> 84     /// 邮箱服务器,如果没有定义邮箱服务器,则根据serviceType和Sender组成邮箱服务器 85     /// </summary> 86     public string Host 87     { 88       get 89       { 90         return string.IsNullOrEmpty(_host) 91           ? (this.ServiceType + "." + 92            Sender.Split("@".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1]) 93           : _host; 94       } 95       set { _host = value; } 96     } 97  98     /// <summary> 99     /// 邮箱账号(默认为发送者邮箱的账号)100     /// </summary>101     public string UserName { get; set; }102 103     /// <summary>104     /// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312105     /// </summary>106     public string Password { get; set; }107 108     /// <summary>109     /// 邮箱优先级110     /// </summary>111     public MailPriority MailPriority { get; set; }112 113     /// <summary>114     /// 邮件正文编码格式115     /// </summary>116     public Encoding Encoding { get; set; }117 118     #endregion119 120     #region ------------------调用方法------------------121 122     /// <summary>123     /// 构造参数,发送邮件,使用方法备注:公开方法中调用124     /// </summary>125     public void Send()126     {127       var mailMessage = new MailMessage();128 129       //读取To 接收者邮箱列表130       if (this.To != null && this.To.Length > 0)131       {132         foreach (string to in this.To)133         {134           if (string.IsNullOrEmpty(to)) continue;135           try136           {137             mailMessage.To.Add(new MailAddress(to.Trim()));138           }139           catch (Exception ex)140           {141             throw new Exception(ex.Message);142           }143         }144       }145       //读取Cc 抄送者邮件地址146       if (this.Cc != null && this.Cc.Length > 0)147       {148         foreach (var cc in this.Cc)149         {150           if (string.IsNullOrEmpty(cc)) continue;151           try152           {153             mailMessage.CC.Add(new MailAddress(cc.Trim()));154           }155           catch (Exception ex)156           {157             throw new Exception(ex.Message);158           }159         }160       }161       //读取Attachments 邮件附件162       if (this.Attachments != null && this.Attachments.Length > 0)163       {164         foreach (var attachment in this.Attachments)165         {166           if (string.IsNullOrEmpty(attachment)) continue;167           try168           {169             mailMessage.Attachments.Add(new Attachment(attachment));170           }171           catch (Exception ex)172           {173             throw new Exception(ex.Message);174           }175         }176       }177       //读取Bcc 秘抄人地址178       if (this.Bcc != null && this.Bcc.Length > 0)179       {180         foreach (var bcc in this.Bcc)181         {182           if (string.IsNullOrEmpty(bcc)) continue;183           try184           {185             mailMessage.Bcc.Add(new MailAddress(bcc.Trim()));186           }187           catch (Exception ex)188           {189             throw new Exception(ex.Message);190           }191         }192       }193       //读取From 发送人地址194       try195       {196         mailMessage.From = new MailAddress(this.From);197       }198       catch (Exception ex)199       {200         throw new Exception(ex.Message);201       }202 203       //邮件标题204       Encoding encoding = Encoding.GetEncoding("GB2312");205       mailMessage.Subject = string.Format("?={0}?B?{1}?=", encoding.HeaderName,206         Convert.ToBase64String(encoding.GetBytes(this.Subject), Base64FormattingOptions.None));207       //邮件正文是否为HTML格式208       mailMessage.IsBodyHtml = this.IsBodyHtml;209       //邮件正文210       mailMessage.Body = this.Body;211       mailMessage.BodyEncoding = this.Encoding;212       //邮件优先级213       mailMessage.Priority = this.MailPriority;214 215       //发送邮件代码实现216       var smtpClient = new SmtpClient217       {218         Host = this.Host,219         Credentials = new NetworkCredential(this.UserName, this.Password)220       };221       //认证222       try223       {224         smtpClient.Send(mailMessage);225       }226       catch (Exception ex)227       {228         throw new Exception(ex.Message);229       }230     }231 232     #endregion233   }234 235   /// <summary>236   ///邮件发送接口调用:bool isTrue=EmailInfo.SendEmail(参数,........);  参数解释参考方法237   /// <auther>238   ///   <name>Kencery</name>239   ///   <date>2015-8-18</date>240   /// </auther>241   /// </summary>242   public static class EmailInfo243   {244     /// <summary>245     /// 邮件发送方法,传递参数(使用中如出现问题,请调试)246     /// </summary>247     /// <param name="from">发送者邮箱名称(从配置文件中读取,比如:934532778@qq.com)(必填项)</param>248     /// <param name="to">接收者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)(必填项)</param>249     /// <param name="cc">抄送者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)</param>250     /// <param name="bcc">秘抄者邮箱列表,可以传递多个,使用string[]表示(从配置文件中读取)</param>251     /// <param name="subject">邮件主题,构造(必填项)</param>252     /// <param name="body">邮件内容,构造发送的邮件内容,可以发送网页(必填项)</param>253     /// <param name="isBodyHtml">是否是HTML格式,true为是,false为否</param>254     /// <param name="attachments">邮箱附件,可以传递多个,使用string[]表示(从配置文件中读取),可空</param>255     /// <param name="host">邮箱服务器(从配置文件中读取,如:smtp@qq.com)(必填项)</param>256     /// <param name="password">邮箱密码(从配置文件中读取,from邮箱的密码)(必填项)</param>257     /// <returns>邮件发送成功,返回true,否则返回false</returns>258     public static bool SendEmail(string from, string[] to, string[] cc, string[] bcc, string subject, string body,259       bool isBodyHtml, string[] attachments, string host, string password)260     {261       //邮箱发送不满足,限制这些参数必须传递262       if (from == "" || to.Length <= 0 || subject == "" || body == "" || host == "" || password == "")263       {264         return false;265       }266 267       var emil = new Email268       {269         From = @from,270         To = to,271         Cc = cc,272         Bcc = bcc,273         Subject = subject,274         Body = body,275         IsBodyHtml = isBodyHtml,276         Attachments = attachments,277         Host = host,278         Password = password279       };280       try281       {282         emil.Send();283         return true;284       }285       catch (Exception ex)286       {287         throw new Exception(ex.Message);288       }289     }290   }291 }

原标题:Smtp邮件发送系统公用代码整理—总结

关键词:邮件

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。