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 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)[])
  93. : _host;
  94. }
  95. set { _host = value; }
  96. }
  97.  
  98. /// <summary>
  99. /// 邮箱账号(默认为发送者邮箱的账号)
  100. /// </summary>
  101. public string UserName { get; set; }
  102.  
  103. /// <summary>
  104. /// 邮箱密码(默认为发送者邮箱的密码),默认格式GB2312
  105. /// </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. #endregion
  119.  
  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 > )
  131. {
  132. foreach (string to in this.To)
  133. {
  134. if (string.IsNullOrEmpty(to)) continue;
  135. try
  136. {
  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 > )
  147. {
  148. foreach (var cc in this.Cc)
  149. {
  150. if (string.IsNullOrEmpty(cc)) continue;
  151. try
  152. {
  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 > )
  163. {
  164. foreach (var attachment in this.Attachments)
  165. {
  166. if (string.IsNullOrEmpty(attachment)) continue;
  167. try
  168. {
  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 > )
  179. {
  180. foreach (var bcc in this.Bcc)
  181. {
  182. if (string.IsNullOrEmpty(bcc)) continue;
  183. try
  184. {
  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. try
  195. {
  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 SmtpClient
  217. {
  218. Host = this.Host,
  219. Credentials = new NetworkCredential(this.UserName, this.Password)
  220. };
  221. //认证
  222. try
  223. {
  224. smtpClient.Send(mailMessage);
  225. }
  226. catch (Exception ex)
  227. {
  228. throw new Exception(ex.Message);
  229. }
  230. }
  231.  
  232. #endregion
  233. }
  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 EmailInfo
  243. {
  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 <= || subject == "" || body == "" || host == "" || password == "")
  263. {
  264. return false;
  265. }
  266.  
  267. var emil = new Email
  268. {
  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 = password
  279. };
  280. try
  281. {
  282. emil.Send();
  283. return true;
  284. }
  285. catch (Exception ex)
  286. {
  287. throw new Exception(ex.Message);
  288. }
  289. }
  290. }
  291. }

Smtp邮件发送系统公用代码整理—总结的更多相关文章

  1. html Css PC 移动端 公用部分样式代码整理

    css常用公用部分样式代码整理: body, html, div, blockquote, img, label, p, h1, h2, h3, h4, h5, h6, pre, ul, ol, li ...

  2. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 334人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  3. C# SMTP邮件发送 分类: C# 2014-07-13 19:10 333人阅读 评论(1) 收藏

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  4. SMTP邮件传输协议发送邮件和附件

    在以前接触的项目中,一直都是在做网站时用到了发送mail 的功能,在asp 和.net 中都有相关的发送mail 的类, 实现起来非常简单.最近这段时间因工作需要在C++ 中使用发送mail 的功能, ...

  5. C# SMTP邮件发送程序

    邮件发送在网站应用程序中经常会用到,包括您现在看到的博客,在添加评论后,系统会自动发送邮件通知到我邮箱的,把系统发送邮件的功能整理了下,做了一个客户端Demo,希望对有需要的童鞋有所帮助: 核心代码: ...

  6. python学习笔记(SMTP邮件发送)

    想着给框架添加邮件发送功能.所以整理下python下邮件发送功能 首先python是支持邮件的发送.内置smtp库.支持发送纯文本.HTML及添加附件的邮件 之后是邮箱.像163.qq.新浪等邮箱默认 ...

  7. [工具-008] C#邮件发送系统

    邮件发送系统很多,但是我这边给大家展示下我最近开发的一款邮件发送系统,有参照网上的一个兄弟的界面,进行了升级,界面如下. 从界面上我们可以看到了该邮件系统有如下功能: 1)服务器的设置 2)发件人的设 ...

  8. python SMTP邮件发送(转载)

    Python SMTP发送邮件 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. py ...

  9. python学习笔记(SMTP邮件发送:带附件)

    博主有段时间没有更新博客了 先整理一个之前整理过的SMTP邮件发送,这次是带附件的功能 #!/usr/bin/env python # -*- coding: utf_8 -*- from email ...

随机推荐

  1. spark MySQL jar 包

    /** * Created by songcl on 2016/6/24. */ import java.sql.DriverManager //val sqlContext = new org.ap ...

  2. MAC自带的SVN进行升级

    1.下载高版本svn:http://www.wandisco.com/subversion/download 2.安装 3. #1.在.bash_profile添加export PATH=/opt/s ...

  3. POJ 2853 Sequence Sum Possibilities

    Sequence Sum Possibilities Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5537   Accep ...

  4. 深入浅出MySQL 数据库开发、优化与管理维护(第2版) -- 读书笔记 -- 基础篇

      1.切换数据库 use blog; 2.显示当前数据库 所有的表. show tables; +----------------+ | Tables_in_blog | +------------ ...

  5. C#通过RFC调用SAP

    using System;using System.Collections.Generic;using SAP.Middleware.Connector;using System.Data;using ...

  6. Exploring Ionic Lists

    Infinite Lists 由于手机不适合使用多页面显示posts,Infinite Lists成为各种新闻.咨询类app的标配.为了在ionic框架中使用到Infinite Lists,我们首先学 ...

  7. C# 读取压缩文件方式及乱码处理

    目前主流的压缩文件操作类,除了C#原生的.还有 1. ZIP-DotNetZip 网址:http://dotnetzip.codeplex.com/ 2 7Zip-SevenZipSharp 网址:h ...

  8. Bower 手册

    安装 Bower 使用 npm 安装 Bower.(Bower 依赖于 Node, npm 和 Git.) $ npm install -g bower 基本用法 安装程序包 程序包安装命令 bowe ...

  9. 在Visual Studio 2010中进行“项目重命名”的有效工具

    地址:http://www.cnblogs.com/dudu/archive/2011/12/11/visual_studio_2010_project_rename.html 提示:这个工具一次 r ...

  10. [OpenCV] Image Processing - Spatial Filtering

    "利用给定像素周围的像素的值决定此像素的最终的输出值“ 教学效果: 策略: 1. 拉普拉斯,突出小细节: . 梯度,突出边缘: . 平滑过的梯度图像用于掩蔽: . 灰度变换,增加灰度动态范围 ...