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. strtr对用户输入的敏感词汇进行过滤

    /** * 过滤用户输入的基本数据,防止script攻击 * * @access public * @return string */ function compile_str($str) { $ar ...

  2. [moka同学摘录]Yii2 csv数据导出扩展

    yii2-thecsv(Yii2框架csv数据导出扩展) github: https://github.com/13552277443/yii2-thecsv 1.安装 运行 php composer ...

  3. Oracle 查询并删除重复记录的SQL语句

    查询及删除重复记录的SQL语句 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select  ...

  4. (HY000): Cannot modify @@session.sql_log_bin inside a transaction

    昨天,线上发生一例(HY000): Cannot modify @@session.sql_log_bin inside a transaction代码缺少显示的start transaction控制 ...

  5. A除以B问题

    描述:本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入:输入在1行中依次给出A和B,中间以1空格分隔. 输出: ...

  6. ES6的Class

    类的基本写法: constructor构造函数其实就相当于ES5中的构造函数,用于定义类的实例属性: 而在类中定义的其他方法像这里的toString方法就相当于ES5中定义在原型prototype上的 ...

  7. 使用WebMatrix发布网站

    使用WebMatrix发布网站 WebMatrix 简介: Microsoft WebMatrix 是微软最新的 Web 开发工具,它包含了构建网站所需要的一切元素.您可以从开源 Web 项目或者内置 ...

  8. Setting up your App domain for SharePoint 2013

    from:http://sharepointchick.com/archive/2012/07/29/setting-up-your-app-domain-for-sharepoint-2013.as ...

  9. android 浏览器开发实例

    android app需要通过手机显示网页信息还是比较常用的,比如我最近业余开发的 抢商铺游戏,需要对游戏规则做说明,规则会比较多,而且要经常变动,就想到用网页来展示,更新起来方便,不像应用,一旦发布 ...

  10. Python开发包推荐系列之xml、html解析器PyQuery

    使用python,喜欢她的简洁是一方面,另外就是它有着丰富的开发包 好用又方便 接下来会给大家推荐一系列很赞的开发包. 在解析html.xml过程中,我们有不少的包可以用.比如bs.lxml.xmlt ...