一、定义邮件发送类

/// <summary>
/// 发送邮件
/// </summary>
public class MailHelper
{
#region 私有变量 private string _mailTo; //接收方
private string _mailFrom; //发送方
private string _subject; //标题
private string _body; //内容
private string _username; //邮件用户
private string _pwd; //密码
private string _smtpclient; //smtp服务器名字
private string _youname; //关联姓名
private bool _bodyhtml = false; //正文是否为html格式
private List<string> _listCc = new List<string>();//抄送
//编码暂硬性规定为GB2312
private Encoding _encoding = Encoding.GetEncoding(); #endregion #region 私有属性 /// <summary>
/// smtp服务器
/// </summary>
private string SmtpClient
{
set
{
//格式如smtp.sina.com
Regex regex = new Regex(@"^smtp(\.([a-zA-Z0-9])+){2}$");
if (regex.IsMatch(value))
this._smtpclient = value;
else
throw new ArgumentException("smtp服务器设置失败");
}
} /// <summary>
/// 发送方
/// </summary>
private string MailTo
{
set
{
if (IsValidEMail(value))
this._mailTo = value;
else
throw new ArgumentException("发送方设置错误");
}
} /// <summary>
/// 用户名
/// </summary>
private string UserName
{
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("用户名不能为空");
if (!this._mailFrom.StartsWith(value))
throw new ArgumentException("用户名设置和发送方不匹配");
this._username = value;
}
} /// <summary>
/// 密码
/// </summary>
private string Pwd
{
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("密码不能为空");
this._pwd = value;
}
} /// <summary>
/// 发送给
/// </summary>
private string MailFrom
{
set
{
if (IsValidEMail(value))
this._mailFrom = value;
else
throw new ArgumentException("接收方设置错误");
}
} /// <summary>
/// 关联姓名
/// </summary>
private string YouName
{
set
{
if (string.IsNullOrEmpty(value))
this._youname = this._username;
else
this._youname = value;
}
} #endregion #region 私有方法 /// <summary>
/// 检测email格式
/// </summary>
/// <param name="email">email</param>
/// <returns>true为正确</returns>
private bool IsValidEMail(string email)
{
Regex regex = new Regex(@"\w+([-+.]\w+)*\w{2,}@\w+([-.]\w+)*\.\w+([-.]\w+)*");
if (regex.IsMatch(email))
return true;
else
return false;
} #endregion #region 公共属性 /// <summary>
/// 标题
/// </summary>
public string Subject
{
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("标题不能为空");
this._subject = value;
}
} /// <summary>
/// 正文
/// </summary>
public string Body
{
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("正文不能为空");
this._body = value;
}
} #endregion #region 公共方法 /// <summary>
/// 构造函数
/// </summary>
/// <param name="smtpclient">smtp服务器,格式如"smtp.sina.com"</param>
/// <param name="mailFrom">发送方,格式如"mymail@sina.com"</param>
/// <param name="username">登入名,必填,注意和发送方匹配</param>
/// <param name="pwd">密码,必填</param>
/// <param name="mailTo">接收方,格式如"youmail@sina.com"</param>
/// <param name="subject">标题,最好别为空</param>
/// <param name="body">正文,必填</param>
/// <param name="bodyhtml">正文是否为html格式</param>
/// <param name="youName">关联姓名, 选填</param>
public MailHelper(string smtpclient, string mailFrom,
string username, string pwd, string mailTo,
string subject, string body,
bool bodyhtml, string youName)
{
this.SmtpClient = smtpclient;
this.MailFrom = mailFrom;
this.UserName = username;
this.Pwd = pwd;
this.MailTo = mailTo;
this.Subject = subject;
this.Body = body;
this.YouName = youName;
this._bodyhtml = bodyhtml;
} /// <summary>
/// 添加抄送邮箱, 可多次调用
/// </summary>
/// <param name="mailCc">抄送的email</param>
public void AddMmailCc(string mailCc)
{
if (IsValidEMail(mailCc))
this._listCc.Add(mailCc);
else
throw new ArgumentException("抄送'" + mailCc + "'地址错误");
} /// <summary>
/// 发送邮件
/// </summary>
/// <returns>true-发送成功</returns>
public void Send()
{
Encoding encoding = this._encoding; MailMessage Message = new MailMessage(
new MailAddress(this._mailFrom, this._youname, this._encoding),
new MailAddress(this._mailTo)); Message.SubjectEncoding = this._encoding;
Message.Subject = this._subject;
Message.BodyEncoding = this._encoding;
Message.Body = this._body;
Message.IsBodyHtml = this._bodyhtml;
foreach (string strCc in _listCc)
{
Message.CC.Add(new MailAddress(strCc));
}
SmtpClient smtpClient = new SmtpClient(this._smtpclient);
smtpClient.Credentials = new NetworkCredential(this._username, this._pwd); smtpClient.Timeout = ;
smtpClient.Send(Message); //异步调用, 避免阻塞
//Timeout 属性对 SendAsync 调用没有影响
//smtpClient.SendAsync(Message, null);
} #endregion
}

二、调用

 MailHelper mail = new MailHelper("smtp.sina.cn", "邮箱帐号@sina.cn", "登录帐号", "登录密码",  "对方邮箱帐号", "邮件标题", "<a href='http://www.baidu.com'>点一点</a>", true, "");
mail.Send();

ps:个人邮箱发送以后,接收方可能会将邮件归到垃圾箱

最新文章

  1. python 类属性与方法
  2. java多线程 生产者消费者模式
  3. Spell-DBC
  4. Ultra Video Splitter &amp; Converter
  5. this Activity.this Activity.class
  6. 【HDU 2063】过山车(二分图匹配)
  7. asp.net core 之静态文件目录的操作
  8. js各种宽高(2)
  9. ecshop---京东手机模板js的eval产生冲突的解决方法。
  10. js到记时代码
  11. svn unable to connect to a repository at url 执行上下文错误 不能访问SVN服务器问题
  12. C#:iterator 迭代器/partial class 分布类/泛型
  13. io的四个分类
  14. tomcat+jdk+mysql
  15. FPGA计算3行同列数据之和
  16. VMware 设置共享目录
  17. zTree实现地市县三级级联Service接口实现
  18. 关于360插件化Replugin Activity动态修改父类的字节码操作
  19. innerHTML innerText与outerHTML间的区别
  20. Python---http协议.md

热门文章

  1. DirectX11 SDK下载地址
  2. mongDB
  3. JS 实现图片直接下载
  4. jvm内存JVM学习笔记-引用(Reference)机制
  5. 理解git分支-远程分支
  6. Redis 集成Spring(spring-data-redis)
  7. Golang爬虫示例包系列教程(一):pedaily.com投资界爬虫
  8. 搭建PHP建站环境
  9. java编程思想笔记(一)——面向对象导论
  10. 使用java连接MySql,中文乱码解决的方法