ASP.NET CORE中发送电子邮件的延迟

问题描述 投票:1回答:1

[他正在使用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("[email protected]", emailDestination, subject,htmlMessageBody);
        ms.IsBodyHtml = true;
        string user = "[email protected]";
        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>.");

我希望在注册后立即发送邮件,以便用户可以确认电子邮件。

[我正在使用blazor构建一个Web应用程序,该应用程序将电子邮件激活链接发送给注册用户,正在发送电子邮件激活,但是这里的问题花费的时间太长(大约5分钟)...

asp.net-core blazor smtpclient
1个回答
0
投票
您似乎没有做任何会产生大量电子邮件的操作,因此此操作不会花费很长时间。我可以提出的建议是在测试环境中设置您的应用程序,并将SMTP连接设置为您可以在配置中访问的电子邮件帐户。 (即使使用gmail帐户也可以,但是您必须适当设置Gmail安全性。)然后,在调试模式下以await smtpServer.SendMailAsync(ms);的断点运行您的应用程序,然后继续(在VS中为F5)从该调用转到执行SendEmail Async(),让应用继续运行。这样可以确认电子邮件已发送,还可以让您深入了解问题是否在完全发送电子邮件之前。在开始之前,请确保您已登录要测试的电子邮件帐户,然后跳至电子邮件帐户“已发送”文件夹并检查其是否显示已发送的电子邮件。如果电子邮件发送时间很长,则问题出在应用程序的SMTP连接中。如果发送顺序很短,但仍然需要花费很长时间才能到达收件人,则它与电子邮件帐户或连接到它们的客户端有关(认为Outlook中的“发送/接收”间隔设置太长),但不是一定是您的应用程序。这应该可以帮助您确定问题所在,以便您了解要处理的问题。
© www.soinside.com 2019 - 2024. All rights reserved.