【问题标题】:Asp.Net Core 3.1 EmailSender is not working in server side after deploying部署后,Asp.Net Core 3.1 EmailSender 在服务器端不起作用
【发布时间】: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,我会尝试拨打asyncawait 进行通话,并且我会记录日志以检查发生了什么
  • @Crowcoder 我同时添加了asyncawait + ILogger 但它正在正常发送电子邮件,日志中没有错误并且电子邮件未到达

标签: c# asp.net-core-3.1


【解决方案1】:

根据 Microsoft “System.Net.Mail” 命名空间已过时,他们提到您可以改用 “MailKit”库

您可以从此链接阅读更多内容:https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netcore-3.1

将 EmailSender 更新为 "MailKit" 后,发送电子邮件工作正常 而且实际上在当时也更可靠。

【讨论】:

    猜你喜欢
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2016-10-04
    相关资源
    最近更新 更多