一、定义邮件发送类

/// <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:个人邮箱发送以后,接收方可能会将邮件归到垃圾箱

C#发送邮件类的更多相关文章

  1. python封装发送邮件类

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart i ...

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

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

  3. PHP功能齐全的发送邮件类

    下面这个类的功能则很强大,不但能发html格式的邮件,还可以发附件 <?php class Email { //---设置全局变量 var $mailTo = ""; // ...

  4. php 发送邮件类

    //******************** 配置信息 ********************************            $smtpserver = "smtp.263 ...

  5. PHP CI框架email类发送邮件

    用CI框架发送邮件类 在中文标题太长的情况下会出现乱码,搜索后说是发送邮件的时候有标题长度的限制,按说的方法修改后,还是没能得到解决,后来发现需要转换邮件标题的编码,解决方法如下: 打开 librar ...

  6. PHP多种形式发送邮件

    1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用 ...

  7. php实现发送邮件

    smtp.php: <?php class smtp {     /* Public Variables */     var $smtp_port;     var $time_out;    ...

  8. PHP中发送邮件的几种方法总结

    1. 使用 mail() 函数 没什么好讲的,就是使用系统自带的smtp系统来发送,一般是使用sendmail来发.这个按照各个系统不同而定.使用参考手册. 2. 使用管道的形式 昨天刚测试成功,使用 ...

  9. 用Java发送邮件

    要用Java发送邮件,除过JDK本身的jar包之外,还需要两个额外的jar包:JavaMail和JAF.当然,如果你使用的JavaEE的JDK,那就不用单独去网上下载了,因为JavaEE的JDK中已经 ...

随机推荐

  1. json-smart 使用示例

    json-smart 使用示例 json是一种通用的数据格式.相比与protocal buffer.thrift等数据格式,json具有可读性强(文本).天生具备良好的扩展性(随意增减字段)等优良特点 ...

  2. 不想作死系列--win7远程linux桌面之vncserver

    1.在linux服务器上安装vncserver yum install vncserver 或者下载相应linux版本的tigervnc-serverrpm rpm -ivh tigervnc-ser ...

  3. MySQL的一些基本查询,创建存储过程等

    常用的查询条件有1.比较:=,<,>,<=,>=,!=,<>,!>,!<              2.确定范围:between and,not bet ...

  4. 设计模式:Prototype 原型模式 - 同学你抄过别人的作业么?-clone()方法的使用

    原型模式: 通过某个类的实例来创建对象 使用原型模式的好处: 好处是什么呢?当我们需要多次重复的创建一个类的示例的时候,我们可以使用new但是,new不仅仅耗费内存而且,如果new 某个类的构造方法中 ...

  5. sql数据黑马程序员——SQL入门

    最近研究sql数据,稍微总结一下,以后继续补充: ---------------------- ASP.Net+Android+IO开辟S..Net培训.等待与您交流! --------------- ...

  6. Python爬虫学习——使用Cookie登录新浪微博

    1.首先在浏览器中进入WAP版微博的网址,因为手机版微博的内容较为简洁,方便后续使用正则表达式或者beautifulSoup等工具对所需要内容进行过滤 https://login.weibo.cn/l ...

  7. 从0到1一步步搭建代码质量检测系统~iOS

    演示环境:Mac OSX10.12.2 Xcode8 先瞄一眼最终成果- 1.JDK,DBMS(演示环境使用Mysql) 2.创建sonar数据库和用户 mysql -u root -pCREATE ...

  8. LIst去重,重写方法,继承接口。

    调用: ]).ToList(); var dic = dataThis.Distinct( new repDic()).ToList();var repList = ""; for ...

  9. MySQL对NULL值的处理

    mysql: 我们已经知道MySQL使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了处理这种情况,M ...

  10. [转]学好Mac常用命令,助力iOS开发

    转自:http://www.jianshu.com/p/d9ec00d28237   序言 在iOS开发的过程中,更多地注重iOS开发的效率,熟练使用Mac终端操作的常用命令,可以让你更好的游刃于iO ...