一、定义邮件发送类

/// <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. asp.net mvc Route 使用自定义条件(constraints)禁止某ip登陆

    asp.net mvc Route 使用自定义条件(constraints)禁止某ip登陆 前言 本文的目的是利用Mvc route创建一个自定义约束来控制路由跳转实现禁止ip登陆,当然例子可能不合理 ...

  2. SL.XNA中的Popup

    如果要xna与sl混合显示,就不能用popup,不然会有各种显示错乱的问题.如果xna与sl单独显示,可以使用popup,但是要记得移除UIElementRenderer.就是说popup只能交给系统 ...

  3. 对Git的一些理解

    使用Git都快2年了,能够说熟练使用git,遇到不会的也可以自己查询git帮助手册.平时可以根据shell的管道命令,组合一些命令比如git show commitID | grep “diff”来看 ...

  4. kivy Grid Layout

    http://kivy.org/docs/api-kivy.uix.gridlayout.html?highlight=gridlayout#kivy.uix.gridlayout It's so n ...

  5. sharepoint 使用命令行注册dll文件到gac的方法

    使用命令行注册dll文件到gac的方法: gacutil.exe -i D:\SPFormLoginProject.dll 删除gac的dll方法: gacutil /u "SPFormLo ...

  6. Linux:用at和crontab调度作业

    一.有2种作业调度方式 1.突发性的,就是只运行作业一次而不是定期运行,使用at命令. 例如在进程A运行一段时间后关闭该进程. 2.定期运行,就是每隔一定的周期运行一次,使用crontab命令. 如每 ...

  7. c语言栈的链表实现

    #include <stdio.h> #include <stdlib.h> #include"PublicDS.h" typedef int ElemTy ...

  8. C++中的异常

    一,异常的推演 1.函数与异常 平时我们在函数中出现异常情况时通常通过return终止函数并返回一个值,然后在函数上层来获取值并判断是什么异常情况.因为函数是栈结构的,所以return的时候是通过栈结 ...

  9. Spark-GraphxAPI学习笔记

    图的集合视图 graph包含三个基本的类集合视图: val vertices: VertexRDD[VD] val edges: EdgeRDD[ED] val triplets: RDD[EdgeT ...

  10. LR日志解析

    在录制和回放的时候,VU会分别把发生的事件记录成日志文件,这些日志有利于我们跟踪VU和服务器的交互过程. 1.回放日志(Replay log) 脚本回放运行时的输出都记在这个log里. "输 ...