【问题标题】:Delay in sending email in ASP.NET CORE在 ASP.NET CORE 中发送电子邮件的延迟
【发布时间】:2020-05-18 10:13:29
【问题描述】:

您好,我正在使用 blazor 构建一个 Web 应用程序,该应用程序向注册用户发送电子邮件激活链接,正在发送电子邮件激活,但这里的问题是注册用户需要很长时间(大约 5 分钟)才能收到激活链接。这是我的代码。

我的接口类

 public interface IEmailServices
{
    Task SendEmailAsync(string toAddress, string subject, string body);
}

我的邮件发件人类别

public class EmailSender : IEmailServices
{
    public async Task SendEmailAsync(string emailDestination, string subject, string htmlMessageBody )
    {

        MailMessage ms = new MailMessage("myemail@domain.com", emailDestination, subject,htmlMessageBody);
        ms.IsBodyHtml = true;
        string user = "myemail@domain.com";
        string passcode = "mypassword";
        SmtpClient smtpServer = new SmtpClient("mail.domain.com");
        smtpServer.Credentials = new System.Net.NetworkCredential(user, passcode);
        try
        {
            await smtpServer.SendMailAsync(ms);
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }       

}

这里是发送消息的地方

//Generate Email confirmation link
                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                        $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

我希望在注册后立即发送消息,以便用户可以确认电子邮件.. 有什么遗漏提前谢谢

【问题讨论】:

    标签: asp.net-core blazor smtpclient


    【解决方案1】:

    您似乎没有做任何会产生大量电子邮件的事情,所以这应该不会花很长时间。我可以提出的建议是在测试环境中设置您的应用程序,并将 SMTP 连接设置为您在配置中可以访问的电子邮件帐户。 (即使是 gmail 帐户也可以使用,但您必须适当地设置 Gmail 安全性。)然后,在调试模式下运行您的应用程序,并在 await smtpServer.SendMailAsync(ms); 处设置断点,然后继续(在 VS 中按 F5)从该调用转发到执行SendEmail Async() 并让应用程序继续运行。这将允许确认电子邮件已发送,并且还可以让您深入了解问题是否在完全发送电子邮件之前。确保您在开始之前登录到您正在测试的电子邮件帐户,然后跳入电子邮件帐户已发送文件夹并检查它是否显示已发送的电子邮件。如果电子邮件需要很长时间才能发送,则问题出在应用程序的 SMTP 连接上。如果它以较短的顺序发送但仍然需要永远到达收件人,它与电子邮件帐户或连接到它们的客户端有关(认为 Outlook 中的发送/接收间隔设置太长),但不是一定是您的应用程序。这应该可以帮助您确定问题,以便您知道自己在处理什么。

    【讨论】:

    • 好的,谢谢,我认为问题出在托管服务提供商 SMTP 上,我登录到邮件服务器并发送了一条消息,发送时间不长,但从我的应用程序需要很长时间。
    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 2010-10-08
    • 2011-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    相关资源
    最近更新 更多