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风格的邮件发送封装类的更多相关文章

  1. Chilkat----开源站点之VS2010 CKMailMan一个很好的邮件发送开源开发包

    Chilkat 是一个很好的开源站点,有各种开源库. 开发语言主要有Classic ASP •C • C++ • C# • Delphi ActiveX • Delphi DLL • Visual F ...

  2. Spring Boot 邮件发送的 5 种姿势!

    邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,使用 JavaSE 代码发送邮件,步骤还是挺繁琐的,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送 ...

  3. 测试开发【提测平台】分享11-Python实现邮件发送的两种方法实践

    微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 按照开发安排,本篇本应该是关于提测页面的搜索和显示实现,怕相似内容疲劳,这期改下内容顺序,将邮件服务的相关的提前,在之前的产品需求和原型中 ...

  4. c# 邮件发送代码分享

    /// <summary> /// 发送邮件方法 /// </summary> /// <param name="sendMail">发送人&l ...

  5. VB.NET的一个邮件发送函数

    ''' <summary> ''' VB.NET邮件发送程序 ''' 还没用在别的服务器,不晓得能不能行,慎用! ''' </summary> ''' <param na ...

  6. 补习系列(12)-springboot 与邮件发送【华为云技术分享】

    目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...

  7. 分享一个php邮件库——swiftmailer

    最近看到一个好的php邮件库,与phpmailer作用一样,但性能比phpmailer好,尤其是在处理附件的能力上,发送邮件成功的几率也高. github地址:https://github.com/s ...

  8. .NET开发邮件发送功能的全面教程(含邮件组件源码)

    今天,给大家分享的是如何在.NET平台中开发“邮件发送”功能.在网上搜的到的各种资料一般都介绍的比较简单,那今天我想比较细的整理介绍下: 1)         邮件基础理论知识 2)         ...

  9. 使用phantomjs实现highcharts等报表通过邮件发送

    使用phantomjs实现highcharts等报表通过邮件发送(本文仅提供完整解决方案和实现思路,完全照搬不去整理代码无法马上得到效果)   前不久项目组需要将测试相关的质量数据通过每日自动生成报表 ...

随机推荐

  1. 机器学习实战 - 读书笔记(13) - 利用PCA来简化数据

    前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第13章 - 利用PCA来简化数据. 这里介绍,机器学习中的降维技术,可简化样品数据. ...

  2. 再说Play!framework http://hsfgo.iteye.com/blog/806974

    这篇帖子的内容我本来想发到 http://www.iteye.com/topic/806660这里的主贴里去的,想挽回被隐藏的命运,但我写完本贴的内容,却发现为时已晚.好吧,我承认,上一个贴的标题容易 ...

  3. 快速理解JS的闭包

    /**闭包:1.在函数内部改变变量值,不影响函数外全局变量(相当于JAVA中私有变量)*        2.调用闭包后,最后产生的变量值并不释放.*        3.任何人调用闭包,闭包里面的值并不 ...

  4. 再谈visibility:hidden和display:none

    之前写过一篇有关visibility:hidden和display:none的文章:为什么要用用visibility:hidden;代替display:none;?主要是从浏览器性能方面入手,却没写两 ...

  5. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath不执行的问题

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPa ...

  6. C编程常见问题总结

    本文是C编程中一些常见错误的总结,有些是显而易见的,有些则是不容易发现 本文地址:http://www.cnblogs.com/archimedes/p/get-screwed-c.html,转载请注 ...

  7. Java集合 之 List 集合

    1.什么是Lsit集合? List集合是一种元素有序的,可重复的集合,集合中每个元素都有自己的元素索引.List集合允许可重复的元素,可以通过索引来访问指定位置的元素. 2.Java8改进的List接 ...

  8. 开始玩mondrian

    官网:http://community.pentaho.com/projects/mondrian/ 官方编译的包:https://sourceforge.net/projects/mondrian/ ...

  9. 深入理解java虚拟机(3)---类的结构

    计算机在开始的时候,只认识0和1,所以汇编语言是和机器结构或者说CPU绑定的.ARM体系结构就是这样一种体现,指令集的概念. 随着高级语言的出现,从字编码发展到了字节编码,计算机的先驱希望能够让语言能 ...

  10. jQuery 安装方法

    在WEB中使用jQuery有两种安装(引入)方式. 1.直接下载jQuery包放置到工程中,然后用js导入的方式连接到WEB页面中 2.利用CDN的公网资源,如百度.新浪等. 访问 http://cd ...