分享一个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等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果) 前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...
随机推荐
- markdown这么好用的东西我才知道。。。多么不折腾的我。。。
markdown 锚点 努力吧 我的网站 之前有个域名phifan.com没续费被抢了,之后又买了phifan.cn没续费被抢了,还剩下个plusnet.cn说什么也不能再丢掉了! package c ...
- 媒体对象 - Media Objects(摘录)
原文链接:http://www.jianshu.com/p/6443be21efbd 一个媒体对象由以下及部分组成 父容器 .media 媒体部分 .media-left 或者 .media-righ ...
- [译] 你应该升级 MQTT3.1.1 的6个理由
原文 6 facts why it’s worth upgrading to the brand new MQTT 3.1.1version 摘要:新版 MQTT 3.1.1 终于在 2014 年 1 ...
- CentOS修改服务器系统时间
linux安装完毕后,一般都是国外的世界,一点都不方便设置任务,或者导致网站获取本地的时间错乱,所以就需要把服务器的时间改为和本地时间一致,也就是换成中国的时间. 第一条指令:date –s '201 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q91-Q93)
Question 91You have a custom user profile property named MyProperty.You need to create a Web Part th ...
- Linux 下Firefox无法打开在'.domain'之前带有中划线的域名
问题 Linux系统下的Firefox无法打开在".domain"之前带有中划线的域名 eg:"http://su---.diandian.com/" 问题原因 ...
- SAM4E单片机之旅——17、通过UART进行标准IO
交互还是很有必要的,而且使用键盘和显示器的交互效率还是很高的.当然,可以直接使用UART进行字符的输入和输出.但是又何必浪费了C的标准输入输出的格式控制之类的功能呢? 这次内容就是使用scanf() ...
- 优秀的PHP开源项目集合
包管理Package Management Package Management Related 框架 框架组件 微框架Micro Frameworks 内容管理系统Content Managemen ...
- Effective Java 18 Prefer interfaces to abstract classes
Feature Interface Abstract class Defining a type that permits multiple implementations Y Y Permitted ...
- forfiles命令批量删除N天前文件
在整理手上几台SQL SERVER 2000的数据库备份时,一方面为了方便快速还原数据库,另外一方面为了备份冗余.备份方式统一(先备份到本地,然后收上磁带),将以前通过Symantec Backup ...