【发布时间】:2020-06-24 16:34:10
【问题描述】:
我使用的是 Asp.Net Core 3.1,并且我使用的是默认的 IEmailSender 微软提供的界面发送电子邮件,不幸的是 部署后无法在服务器端工作
在我的代码中,我尝试通过本地主机发送电子邮件,它正常工作并完美发送电子邮件然后我部署了系统,但是,当我需要通过服务器中托管的应用程序发送电子邮件,发送电子邮件不起作用,我的意思是代码正在工作并更新信息,但是,电子邮件没有到达。
这是继承自IEmailSender 接口的EmailSender 类示例:
public EmailSender(string host, int port, bool enableSSL, string userName, string password)
{
this.host = host;
this.port = port;
this.enableSSL = enableSSL;
this.userName = userName;
this.password = password;
}
public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
var client = new SmtpClient(host, port)
{
Credentials = new NetworkCredential(userName, password),
EnableSsl = enableSSL
};
// To avoid any error come from domain email server
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
client.SendMailAsync(
new MailMessage(userName, email, subject, htmlMessage) { IsBodyHtml = true, Priority = MailPriority.High }
);
//client.Send(
// new MailMessage(userName, email, subject, htmlMessage) { IsBodyHtml = true }
// );
return Task.CompletedTask;
}
知道我在服务器端测试通过 telnet 发送电子邮件时检查了安全性并且工作正常,如何允许通过服务器发送电子邮件。
【问题讨论】:
-
您可以尝试使方法异步并
awaiting 调用SendMainAsync。您的请求可能在任务运行之前就结束了,因为您正在做的是“即发即弃”。另外,您有任何异常处理和日志记录吗? -
谢谢@Crowcoder,我会尝试拨打
async和await进行通话,并且我会记录日志以检查发生了什么 -
@Crowcoder 我同时添加了
async和await+ILogger但它正在正常发送电子邮件,日志中没有错误并且电子邮件未到达
标签: c# asp.net-core-3.1