一,邮箱账号相关设置

1,创建outLook邮箱。

2,进入邮箱设置->同步电子邮件->允许设备和应用使用pop

3,设置microsoft账号的应用程序密码->进入安全性页面->更多安全选项->双层验证功能设置

4,双层验证功能设置好后页面会出现 应用密码 项,创建新应用密码

二,代码

1,NuGet添加FluentEmail引用

2,config文件添加邮件配置

  "Email": {
"EmailAddress": "[email protected]",
"Host": "smtp.office365.com",
"From": "[email protected]",
"Name": "无锡哈哈",
"Password": "",//上一步中创建的应用密码
"Port":
},

3,ConfigureServices中读取配置

                var cemail = Configuration.GetSection("Email").Get<CompanyEmail>();

                services
.AddFluentEmail(cemail.EmailAddress, cemail.Name)
.AddSmtpSender(new SmtpClient()
{
Host = cemail.Host,
EnableSsl = true,
UseDefaultCredentials = false,
Port=cemail.Port,
Credentials = new NetworkCredential(cemail.EmailAddress, cemail.Password)
});

4,发送邮件

        public ActionResult Sendwlemail([FromServices]IFluentEmail fluentEmail)
{
try
{ string toMail="[email protected]";
SendResponse resp= fluentEmail.To(toMail)
.Subject("邮件标题")
.Body("邮件内容")
.Send();
if (resp.Successful)
{ return Success();
} var error = resp.ErrorMessages.ToJson();
LogNHelper.Warn(error);
return Error("Send Failure!");
}
catch (Exception e)
{
LogNHelper.Exception(e);
return Error("Send Failure!");
} }

5,Over

05-28 00:58