分享一个Fluent风格的邮件发送封装类
C#中用SmtpClient发邮件很简单,闲着无事,简单封装一下
IEmailFactory
- public interface IEmailFactory
- {
- IEmailFactory SetHost(string host);
- IEmailFactory SetPort(int port);
- IEmailFactory SetUserName(string userName);
- IEmailFactory SetPassword(string password);
- IEmailFactory SetSSL(bool enableSsl);
- IEmailFactory SetTimeout(int timeout);
- IEmailFactory SetFromAddress(string address);
- IEmailFactory SetFromDisplayName(string displayName);
- IEmailFactory LoadFromConfigFile(); //从Config文件中加载配置
- IEmailFactory SetSubject(string subject);
- IEmailFactory SetBody(string body);
- /// <summary>
- /// 添加收件人地址(执行多次即添加多个地址)
- /// </summary>
- IEmailFactory SetToAddress(params string[] addresses);
- /// <summary>
- /// 添加抄送人地址(执行多次即添加多个地址)
- /// </summary>
- IEmailFactory SetCcAddress(params string[] addresses);
- /// <summary>
- /// 添加附件(执行多次即添加多个附件)
- /// </summary>
- IEmailFactory SetAttachment(params Attachment[] attachments);
- void Send();
- Task SendAsync();
- }
EmailFactory
- class EmailFactory : IEmailFactory
- {
- #region properties
- protected string Host { get; set; }
- protected int Port { get; set; }
- protected string UserName { get; set; }
- protected string Password { get; set; }
- protected bool EnableSSL { get; set; }
- protected int? Timeout { get; set; }
- protected string FromAddress { get; set; }
- protected string FromDisplayName { get; set; }
- protected string Subject { get; set; }
- protected string Body { get; set; }
- protected IList<string> ToList { get; set; }
- protected IList<string> CcList { get; set; }
- protected IList<Attachment> Attachments { get; set; }
- #endregion
- #region initial methods
- public IEmailFactory SetHost(string host)
- {
- this.Host = host;
- return this;
- }
- public IEmailFactory SetPort(int port)
- {
- this.Port = port;
- return this;
- }
- public IEmailFactory SetSSL(bool enableSsl)
- {
- this.EnableSSL = enableSsl;
- return this;
- }
- public IEmailFactory SetTimeout(int timeout)
- {
- this.Timeout = timeout;
- return this;
- }
- public IEmailFactory SetUserName(string userName)
- {
- this.UserName = userName;
- return this;
- }
- public IEmailFactory SetPassword(string password)
- {
- this.Password = password;
- return this;
- }
- public IEmailFactory SetFromAddress(string address)
- {
- this.FromAddress = address;
- return this;
- }
- public IEmailFactory SetFromDisplayName(string displayName)
- {
- this.FromDisplayName = displayName;
- return this;
- }
- public IEmailFactory LoadFromConfigFile()
- {
- var section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;
- this.Host = section.Network.Host;
- this.Port = section.Network.Port;
- this.EnableSSL = section.Network.EnableSsl;
- this.UserName = section.Network.UserName;
- this.Password = section.Network.Password;
- this.FromAddress = section.From;
- return this;
- }
- public IEmailFactory SetSubject(string subject)
- {
- this.Subject = subject;
- return this;
- }
- public IEmailFactory SetBody(string body)
- {
- this.Body = body;
- return this;
- }
- public IEmailFactory SetToAddress(params string[] addresses)
- {
- if (this.ToList == null) this.ToList = new List<string>();
- if (addresses != null)
- foreach (var item in addresses)
- this.ToList.Add(item);
- return this;
- }
- public IEmailFactory SetCcAddress(params string[] addresses)
- {
- if (this.CcList == null) this.CcList = new List<string>();
- if (addresses != null)
- foreach (var item in addresses)
- this.CcList.Add(item);
- return this;
- }
- public IEmailFactory SetAttachment(params Attachment[] attachments)
- {
- if (this.Attachments == null) this.Attachments = new List<Attachment>();
- if (attachments != null)
- foreach (var item in attachments)
- this.Attachments.Add(item);
- return this;
- }
- #endregion
- public virtual void Send()
- {
- using (SmtpClient smtp = new SmtpClient(this.Host, this.Port))
- {
- var message = PreSend(smtp);
- smtp.Send(message);
- }
- }
- public virtual async Task SendAsync()
- {
- using (SmtpClient smtp = new SmtpClient(this.Host, this.Port))
- {
- var message = PreSend(smtp);
- await smtp.SendMailAsync(message);
- }
- }
- private MailMessage PreSend(SmtpClient smtp)
- {
- if (this.UserName != null && this.Password != null)
- {
- smtp.UseDefaultCredentials = false;
- smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
- smtp.Credentials = new System.Net.NetworkCredential(this.UserName, this.Password);
- }
- if (this.Timeout == null)
- smtp.Timeout = ;
- var message = new MailMessage();
- message.From = new MailAddress(this.FromAddress, this.FromDisplayName, Encoding.UTF8);
- if (this.ToList != null)
- foreach (var address in this.ToList)
- message.To.Add(address);
- if (this.CcList != null)
- foreach (var address in this.CcList)
- message.CC.Add(address);
- if (this.Attachments != null)
- foreach (var attachment in this.Attachments)
- message.Attachments.Add(attachment);
- message.Subject = this.Subject;
- message.SubjectEncoding = Encoding.UTF8;
- message.Body = this.Body;
- message.IsBodyHtml = true;
- message.BodyEncoding = Encoding.UTF8;
- message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- return message;
- }
- }
EmailWrapper
- public class EmailWrapper
- {
- private static readonly EmailHelper _instance = new EmailHelper();
- private EmailHelper() { }
- public static IEmailFactory Initalize
- {
- get { return _instance.GetFactory(); }
- }
- private IEmailFactory GetFactory()
- {
- return new EmailFactory();
- }
- }
使用方法:
- //同步发送
- EmailWrapper.Initalize
- .SetHost("smtp.xxxxx.com")
- .SetPort()
- .SetUserName("xxx@xxxxx.com")
- .SetPassword("******")
- .SetSSL(false)
- .SetFromAddress("xxx@xxxxx.com")
- .SetFromDisplayName("Felix")
- .SetToAddress("f5.zhang@qq.com", "f5.lee@gmail.com")
- .SetCcAddress("f5.chow@yahoo.com")
- .SetSubject("会员注册成功")
- .SetBody("恭喜你成为会员,为了你的账号安全,请尽快前往安全中心修改登录密码。")
- .Send();
- //异步发送 从CONFIG中加载配置
- await EmailWrapper.Initalize
- .LoadFromConfigFile()
- .SetFromDisplayName("Felix")
- .SetToAddress("f5.zhang@qq.com")
- .SetToAddress("f5.lee@gmail.com")
- .SetToAddress("f5.chow@yahoo.com")
- .SetSubject("会员注册成功")
- .SetBody("恭喜你成为会员,为了你的账号安全,请尽快前往安全中心修改登录密码。")
- .SendAsync();
分享一个Fluent风格的邮件发送封装类的更多相关文章
- Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包
Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...
- Spring Boot 邮件发送的 5 种姿势!
邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送 ...
- 测试开发【提测平台】分享11-Python实现邮件发送的两种方法实践
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 按照开发安排,本篇本应该是关于提测页面的搜索和显示实现,怕相似内容疲劳,这期改下内容顺序,将邮件服务的相关的提前,在之前的产品需求和原型中 ...
- c# 邮件发送代码分享
/// <summary> /// 发送邮件方法 /// </summary> /// <param name="sendMail">发送人&l ...
- VB.NET的一个邮件发送函数
''' <summary> ''' VB.NET邮件发送程序 ''' 还没用在别的服务器,不晓得能不能行,慎用! ''' </summary> ''' <param na ...
- 补习系列(12)-springboot 与邮件发送【华为云技术分享】
目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...
- 分享一个php邮件库——swiftmailer
最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送邮件成功的几率也高. github地址:https://github.com/s ...
- .NET开发邮件发送功能的全面教程(含邮件组件源码)
今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1) 邮件基础理论知识 2) ...
- 使用phantomjs实现highcharts等报表通过邮件发送
使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果) 前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...
随机推荐
- 泛函编程(20)-泛函库设计-Further Into Parallelism
上两节我们建了一个并行运算组件库,实现了一些基本的并行运算功能.到现在这个阶段,编写并行运算函数已经可以和数学代数解题相近了:我们了解了问题需求,然后从类型匹配入手逐步产生题解.下面我们再多做几个练习 ...
- Linux命令详解之—cat命令
cat命令的功能是连接文件或标准输入并打印,今天就为大家介绍下Linux中的cat命令. 更多Linux命令详情请看:Linux命令速查手册 Linux 的cat命令通常用来显示文件内容,也可以用来将 ...
- 转载 教你使用PS来制作unity3D随机地形
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了 ps -A | gr ...
- [下载] MultiBeast 6.2.1版,支持10.9 Mavericks。Mac上的驱动精灵,最简单安装驱动的方式。
下载地址1:http://pan.baidu.com/s/1i3ier9F 下载地址2:http://www.tonymacx86.com/downloads.php?do=cat&id=3 ...
- Chrome使用技巧(几个月的心得)
转用Chrome,不仅仅因为它的插件之丰富,更因为它的响应速度其他浏览器都望尘莫及.接着我就要写写一些心得. 如何最简易地用上谷歌搜索? 1,下载hosts文件:https://pan.baidu.c ...
- 移动端H5---页面适配问题详谈(一)
一.前言 昨天唠叨了哈没用的,今天说点有用的把.先说一下响应式布局吧,我一直认为响应式布局的分项目,一下布局简单得项目做响应式还是可以可以得.例如博客.后台管理系统等.但是有些会认为响应式很牛逼,尤其 ...
- ASP.NET MVC 微信公共平台开发之获取用户消息并处理
ASP.NET MVC 微信公共平台开发 获取用户消息并处理 获取用户消息 用户发送的消息是在微信服务器发送的一个HTTP POST请求中包含的,获取用户发送的消息要从POST请求的数据流中获取 微信 ...
- ogrinfo使用
简介 orginfo是OGR模块中提供的一个重要工具,用于读取地图文件中记录,可以指定筛选条件(按字段.sql.矩形范围) 使用方式 命令行参数 Usage: ogrinfo [--help-gene ...
- iOS设计模式之单例模式
单例模式 基础理解 所有类都有构造方法,不编码则系统默认生成空的构造方法,若有显示定义的构造方法,默认的构造方法就会失效. 单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全 ...