using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
namespace NFine.Code
{
public class MailHelper
{
///
/// 邮件服务器地址
///
public string MailServer { get; set; }
///
/// 用户名
///
public string MailUserName { get; set; }
///
/// 密码
///
public string MailPassword { get; set; }
///
/// 名称
///
public string MailName { get; set; }
///
/// 端口
///
public int Post { get; set; }
public MailHelper(string _MailServer,string _MailUserName,string _MailPassword,string _MailName,int _Post)
{
MailServer = _MailServer;
MailUserName = _MailUserName;
MailPassword = _MailPassword;
MailName = _MailName;
Post = _Post;
}
///
/// 同步发送邮件
///
/// 收件人邮箱地址
/// 主题
/// 内容
/// 编码
/// 是否Html
/// 是否SSL加密连接
/// 是否成功
public bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
{
try
{
MailMessage message = new MailMessage();
// 接收人邮箱地址
if (!string.IsNullOrEmpty(to))
{
string[] TOList = to.Split(',');
foreach(string item in TOList)
{
message.To.Add(new MailAddress(item));
}
}
message.From = new MailAddress(MailUserName, MailName);
message.BodyEncoding = Encoding.GetEncoding(encoding);
message.Body = body;
//GB2312
message.SubjectEncoding = Encoding.GetEncoding(encoding);
message.Subject = subject;
message.IsBodyHtml = isBodyHtml;
SmtpClient smtpclient = new SmtpClient(MailServer, Post);
smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
//SSL连接
smtpclient.EnableSsl = enableSsl;
smtpclient.Send(message);
LogEntity logEntity = new LogEntity();
logEntity.F_ModuleName = "发邮件";
logEntity.F_Type = "SendMail";
var LoginInfo = OperatorProvider.Provider.GetCurrent();
logEntity.F_Account = LoginInfo.UserCode;
logEntity.F_NickName = LoginInfo.UserName;
logEntity.F_Result = false;
logEntity.F_Description = "发邮件成功";
new LogApp().WriteDbLog(logEntity);
return true;
}
catch (Exception ex)
{
LogEntity logEntity = new LogEntity();
logEntity.F_ModuleName = "发邮件";
logEntity.F_Type = "SendMail";
var LoginInfo = OperatorProvider.Provider.GetCurrent();
logEntity.F_Account = LoginInfo.UserCode;
logEntity.F_NickName = LoginInfo.UserName;
logEntity.F_Result = false;
logEntity.F_Description = "发邮件失败:"+ex.Message;
new LogApp().WriteDbLog(logEntity);
throw;
}
}
///
/// 异步发送邮件 独立线程
///
/// 邮件接收人
/// 邮件标题
/// 邮件内容
/// 端口号
///
public void SendByThread(string to, string title, string body, int port = 25)
{
new Thread(new ThreadStart(delegate()
{
try
{
SmtpClient smtp = new SmtpClient();
//邮箱的smtp地址
smtp.Host = MailServer;
//端口号
smtp.Port = port;
//构建发件人的身份凭据类
smtp.Credentials = new NetworkCredential(MailUserName, MailPassword);
//构建消息类
MailMessage objMailMessage = new MailMessage();
//设置优先级
objMailMessage.Priority = MailPriority.High;
//消息发送人
objMailMessage.From = new MailAddress(MailUserName, MailName, System.Text.Encoding.UTF8);
//收件人
objMailMessage.To.Add(to);
//标题
objMailMessage.Subject = title.Trim();
//标题字符编码
objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
//正文
objMailMessage.Body = body.Trim();
objMailMessage.IsBodyHtml = true;
//内容字符编码
objMailMessage.BodyEncoding = System.Text.Encoding.UTF8;
//发送
smtp.Send(objMailMessage);
}
catch (Exception)
{
throw;
}
})).Start();
}
}
}