using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace learun.util
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.11.05
/// 描 述:发送邮件
///
public static class MailHelper
{
///
/// 邮件服务器地址
///
private static readonly string MailServer = ConfigHelper.GetConfig().MailHost;
///
/// 用户名
///
private static readonly string MailUserName = ConfigHelper.GetConfig().MailUserName;
///
/// 密码
///
private static readonly string MailPassword = ConfigHelper.GetConfig().MailPassword;
///
/// 名称
///
private static readonly string MailName = ConfigHelper.GetConfig().MailName;
///
/// 发送邮件
///
/// 收件人邮箱地址
/// 主题
/// 内容
/// 编码
/// 是否Html
/// 是否SSL加密连接
/// 是否成功
public static async Task Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
{
MailMessage mailMsg = new MailMessage
{
From = new MailAddress(MailUserName, MailName) //源邮件地址和发件人
};//实例化对象
mailMsg.To.Add(new MailAddress(to));//收件人地址
mailMsg.SubjectEncoding = Encoding.GetEncoding(encoding);
mailMsg.Subject = subject;
mailMsg.IsBodyHtml = isBodyHtml;
//指定smtp服务地址(根据发件人邮箱指定对应SMTP服务器地址)
SmtpClient smtpclient = new SmtpClient(MailServer, 25)
{
//加密
EnableSsl = true,
//通过用户名和密码验证发件人身份
Credentials = new NetworkCredential(MailUserName, MailPassword)
};
await smtpclient.SendMailAsync(mailMsg);
}
}
}