1. 配置文件 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- Smtp 服务器 -->
<add key="SmtpHost" value="smtp.qq.com" />
<!-- Smtp 服务器端口 -->
<add key="SmtpPort" value="25" />
<!--发送者 Email 地址-->
<add key="FromEmailAddress" value="xx@qq.com" />
<!--发送者 Email 密码-->
<add key="FormEmailPassword" value="??????" />
</appSettings>
</configuration>

2.帮助类代码

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text; namespace ConsoleApplication1
{
/// <summary>
/// Author : yenange
/// Date : 2014-02-26
/// Description : 邮件发送辅助类
/// </summary>
public class EmailHelper
{
#region [ 属性(发送Email相关) ]
private string _SmtpHost = string.Empty;
private int _SmtpPort = -1;
private string _FromEmailAddress = string.Empty;
private string _FormEmailPassword = string.Empty; /// <summary>
/// smtp 服务器
/// </summary>
public string SmtpHost
{
get
{
if (string.IsNullOrEmpty(_SmtpHost))
{
_SmtpHost = ConfigurationManager.AppSettings["SmtpHost"];
}
return _SmtpHost;
}
}
/// <summary>
/// smtp 服务器端口 默认为25
/// </summary>
public int SmtpPort
{
get
{
if (_SmtpPort == -1)
{
if (!int.TryParse(ConfigurationManager.AppSettings["SmtpPort"], out _SmtpPort))
{
_SmtpPort = 25;
}
}
return _SmtpPort;
}
}
/// <summary>
/// 发送者 Eamil 地址
/// </summary>
public string FromEmailAddress
{
get
{
if (string.IsNullOrEmpty(_FromEmailAddress))
{
_FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"];
}
return _FromEmailAddress;
}
} /// <summary>
/// 发送者 Eamil 密码
/// </summary>
public string FormEmailPassword
{
get
{
if (string.IsNullOrEmpty(_FormEmailPassword))
{
_FormEmailPassword = ConfigurationManager.AppSettings["FormEmailPassword"];
}
return _FormEmailPassword;
}
}
#endregion #region [ 属性(邮件相关) ]
/// <summary>
/// 收件人 Email 列表,多个邮件地址之间用 半角逗号 分开
/// </summary>
public string ToList { get; set; }
/// <summary>
/// 邮件的抄送者,支持群发,多个邮件地址之间用 半角逗号 分开
/// </summary>
public string CCList { get; set; }
/// <summary>
/// 邮件的密送者,支持群发,多个邮件地址之间用 半角逗号 分开
/// </summary>
public string BccList { get; set; }
/// <summary>
/// 邮件标题
/// </summary>
public string Subject { get; set; }
/// <summary>
/// 邮件正文
/// </summary>
public string Body { get; set; } private bool _IsBodyHtml = true;
/// <summary>
/// 邮件正文是否为Html格式
/// </summary>
public bool IsBodyHtml
{
get { return _IsBodyHtml; }
set { _IsBodyHtml = value; }
}
/// <summary>
/// 附件列表
/// </summary>
public List<Attachment> AttachmentList { get; set; }
#endregion #region [ 构造函数 ]
/// <summary>
/// 构造函数 (body默认为html格式)
/// </summary>
/// <param name="toList">收件人列表</param>
/// <param name="subject">邮件标题</param>
/// <param name="body">邮件正文</param>
public EmailHelper(string toList, string subject, string body)
{
this.ToList = toList;
this.Subject = subject;
this.Body = body;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toList">收件人列表</param>
/// <param name="subject">邮件标题</param>
/// <param name="isBodyHtml">邮件正文是否为Html格式</param>
/// <param name="body">邮件正文</param>
public EmailHelper(string toList, string subject, bool isBodyHtml, string body)
{
this.ToList = toList;
this.Subject = subject;
this.IsBodyHtml = isBodyHtml;
this.Body = body;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toList">收件人列表</param>
/// <param name="ccList">抄送人列表</param>
/// <param name="bccList">密送人列表</param>
/// <param name="subject">邮件标题</param>
/// <param name="isBodyHtml">邮件正文是否为Html格式</param>
/// <param name="body">邮件正文</param>
public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body)
{
this.ToList = toList;
this.CCList = ccList;
this.BccList = bccList;
this.Subject = subject;
this.IsBodyHtml = isBodyHtml;
this.Body = body;
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="toList">收件人列表</param>
/// <param name="ccList">抄送人列表</param>
/// <param name="bccList">密送人列表</param>
/// <param name="subject">邮件标题</param>
/// <param name="isBodyHtml">邮件正文是否为Html格式</param>
/// <param name="body">邮件正文</param>
/// <param name="attachmentList">附件列表</param>
public EmailHelper(string toList, string ccList, string bccList, string subject, bool isBodyHtml, string body, List<Attachment> attachmentList)
{
this.ToList = toList;
this.CCList = ccList;
this.BccList = bccList;
this.Subject = subject;
this.IsBodyHtml = isBodyHtml;
this.Body = body;
this.AttachmentList = attachmentList;
}
#endregion #region [ 发送邮件 ]
/// <summary>
/// 发送邮件
/// </summary>
/// <returns></returns>
public void Send()
{
SmtpClient smtp = new SmtpClient(); //实例化一个SmtpClient
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network
smtp.EnableSsl = false; //smtp服务器是否启用SSL加密
smtp.Host = this.SmtpHost; //指定 smtp 服务器地址
smtp.Port = this.SmtpPort; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去
smtp.UseDefaultCredentials = true; //如果你的SMTP服务器不需要身份认证,则使用下面的方式,不过,目前基本没有不需要认证的了
smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword); //如果需要认证,则用下面的方式 MailMessage mm = new MailMessage(); //实例化一个邮件类
mm.Priority = MailPriority.Normal; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可
mm.From = new MailAddress(this.FromEmailAddress, "管理员", Encoding.GetEncoding(936)); //收件人
if (!string.IsNullOrEmpty(this.ToList))
mm.To.Add(this.ToList);
//抄送人
if (!string.IsNullOrEmpty(this.CCList))
mm.CC.Add(this.CCList);
//密送人
if (!string.IsNullOrEmpty(this.BccList))
mm.Bcc.Add(this.BccList); mm.Subject = this.Subject; //邮件标题
mm.SubjectEncoding = Encoding.GetEncoding(936); //这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。
mm.IsBodyHtml = this.IsBodyHtml; //邮件正文是否是HTML格式
mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码
mm.Body = this.Body; //邮件正文
//邮件附件
if (this.AttachmentList != null && this.AttachmentList.Count > 0)
{
foreach (Attachment attachment in this.AttachmentList)
{
mm.Attachments.Add(attachment);
}
}
//发送邮件,如果不返回异常, 则大功告成了。
smtp.Send(mm);
}
#endregion
}
}

3.测试代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Configuration; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
EmailHelper email = new EmailHelper("xxx@qq.com,xxx@qq.com", "测试邮件2", "<html><body><div style='color:red;'>测试内容</div></body></html>");
email.Send();
Console.WriteLine("发送成功!");
}
catch (Exception ex)
{
Console.WriteLine("发送失败!失败原因:");
Console.WriteLine(ex.Message);
}
Console.Read();
}
}//end of class Program
}//end of namespace

C# Email 帮助类 EmailHelper的更多相关文章

  1. .Net Email操作类

    using System; using System.Text; using System.Net.Mail; using System.Net; using System.Linq; using S ...

  2. asp.net C#发送邮件类

    很久前写的一个简单邮件发送类分享给大家: using System; using System.Data; using System.Configuration; using System.Web; ...

  3. spring使用Email邮件系统

    1.提供邮件信息发送接收,附件绑定功能. 1.配置spring-email.xml文件 <context:property-placeholder location="classpat ...

  4. Java类的设计----Object 类

    Object类 Object类是所有Java类的根父类如果在类的声明中未使用extends关键字指明其父类,则默认父类为Object类 public class Person { ... } 等价于: ...

  5. php异常处理类

    <?php header('content-type:text/html;charset=UTF-8'); // 创建email异常处理类 class emailException extend ...

  6. java邮件工具类【最终版】

    http://www.xdemo.org/java-mail/ 对比链接中,添加了抄送和暗抄送功能(已解决,如图代码:抄送不能多个用户,会报错,未解之谜) sendHtmlmail方法可以发送附件以及 ...

  7. android Email总结文档

    目录:src\com.android.email.activity 一. Welcome.java 根据AndroidManifest.xml可知该文件为程序入口文件: 加载该文件时,查询数据库账户列 ...

  8. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  9. [Java] JavaMail 发送带图片的 html 格式的邮件

    JavaMail 发送的邮件正文和附件是相互独立的,但是内置图片需要定位图片在正文中的位置,所以内置图片和邮件正文是互相依赖的. 发送带附件的邮件可参考JavaMail 发送 html 格式.带附件的 ...

随机推荐

  1. IL角度理解for 与foreach的区别——迭代器模式

    IL角度理解for 与foreach的区别--迭代器模式 目录 IL角度理解for 与foreach的区别--迭代器模式 1 最常用的设计模式 1.1 背景 1.2 摘要 2 遍历元素 3 删除元素 ...

  2. Python 中 'unicodeescape' codec can't decode bytes in position XXX: trun错误解决方案

    window 读取文件可以用\,但是在字符串中\是被当作转义字符来使用,'C:\Users\FrankYuan\Pictures\Camera Roll\WIN_20161010_08_51_57_P ...

  3. Vue单元测试vue2-jest-coverage的package.json 配置

     依赖的版本很重要,不要出错了 devDependencies:{ "babel-core": "^6.26.3", "babel-jest" ...

  4. kettle——转换案例

    把stu1的数据按id同步到stu2,stu2有相同id则更新数据 (1)在mysql中创建两张表 mysql> create database kettle; mysql> use ke ...

  5. 如何使用GitHub创建Maven私有仓库

    [Github上创建仓库] 首先,在GitHub上创建自己的仓库(mvn-repo): [配置本地setting文件] 找到本地的maven settings文件,配置server: 有两种选择,可以 ...

  6. [Kafka][1][初识Kafka]

    目录 第1章 初识Kafka 1.1 发布与订阅消息系统 1.1.1 如何开始 1.1.2 独立的队列系统 1.2 Kafka登场 1.2.1 消息和批次(Message and batch) 1.2 ...

  7. 每天一个linux命令之stat

    [Linux]Linux下使用stat命令所显示出来的三个时间 转 https://blog.csdn.net/pointer_y/article/details/54347968 在linux系统下 ...

  8. php7的Opcache getshell

    OPcache基础 OPcache是一种通过解析的PHP脚本预编译的字节码存放在共享内存中来避免每次加载和解析PHP脚本的开销,解析器可以直接从共享内存读取已经缓存的字节码,从而大大提高了PHP的执行 ...

  9. phpstudy搭建网站只能访问首页,其他路由访问404

    今天博主遇到了一个很奇葩的问题,电脑下载了一个phpstudy搭建网站,框架用的是tp,但是除了输入域名能访问,其他页面都访问不了 经过博主的疯狂问大佬,以及百度,终于解决了这个问题 这次出现问题的原 ...

  10. kali 系列学习10-渗透攻击MySQL数据库服务、PostgreSQL数据库服务、Tomcat服务和PDF文件

    介绍使用MSFCONSOLE工具渗透攻击MySQL数据库服务.PostgreSQL数据库服务.Tomcat服务和PDF文件等. 6.4.1 渗透攻击MySQL数据库服务 MySQL是一个关系型数据库管 ...