问题描述
我有一个带有电子邮件客户端的 .net 核心应用程序。 本地运行一切正常,邮件是带附件发送的,但是当我部署 API 时,我总是在尝试发送电子邮件时收到相同的错误。
这是我的代码。 api 从应用设置文件中接收了我的所有设置。
cigarMsg.IsBodyHtml = true;
var mailClient = new SmtpClient(_config.GetValue<string>("SMTPHost"),_config.GetValue<int>("SMTPPort"))
{
UseDefaultCredentials = false,Credentials =
new NetworkCredential("rokokoen@gmail.com",_config.GetValue<string>("PasswordSender")),EnableSsl = true
};
writer.Write(orderPdf);
writer.Flush();
memoryStream.Position = 0;
var attachment = new Attachment(new MemoryStream(orderPdf),"Bestelling-bvbaRomboutEdiers.pdf",System.Net.Mime.MediaTypeNames.Application.Pdf);
cigarMsg.Attachments.Add(attachment);
try
{
mailClient.Send(cigarMsg);
memoryStream.dispose();
writer.dispose();
}
catch (Exception e)
{
Console.WriteLine(e);
Log.Error(e,"Host terminated unexpectedly.");
throw;
}
我的 API 的应用设置
我知道 SMTPPort 应该是 int,但我将类型转换为 int,正如您在我的代码中看到的那样。 是的,这是在本地工作。
我还搜索了以前的 Stackoverflow 问题,但没有任何效果。 我尝试了其他堆栈溢出问题的所有方法,但没有任何帮助。
我收到的错误是:
有人可以帮我解决这个问题吗?
解决方法
SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 需要身份验证:
按可能提供帮助的解决方案排序。
- 检查用户是否启用了 2fa,如果是,您将需要一个 apps password
- 检查您的Captcha loc
- 查看Xoauth2
显然,使用 Gmail 帐户比使用 Hotmail 帐户困难得多。 所以我切换了我的 SMTP 主机和端口。
现在可以发送电子邮件了。
感谢您的帮助。