Delay in sending email in ASP.NET CORE

Viewed 673

Hi am building a web application using blazor which sends email activation link to registered users, email activation is is being sent but the problem here it takes too long(approximately 5 minutes) for the registered user to receive the activation link. here is my code.

my interface class

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

My mail Sender Class

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;
        }

    }       

}

Here's where am sending the message

//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>.");

I want the message to be sent immediately upon registration so user can confirm email.. is there something am missing thanks in advance

1 Answers

You don't appear to be doing anything that would generate a huge email, so this shouldn't be taking very long. A suggestion I can make is to set up your app in a test environment, with the SMTP connection set to an email account you have access to in your configuration. (Even a gmail account would work, but you'd have to set the Gmail security appropriately.) Then, run your app in debug mode with a breakpoint at await smtpServer.SendMailAsync(ms);, and then continue (F5 in VS) forward from that call to execute SendEmail Async() and let the app continue running. This will allow confirmation that the email sent, and also give you some insight into if the issue is ahead of sending the email entirely or not. Make sure you are signed in to the email account you are testing with before you start, then hop into the email account Sent folder and check that it shows the sent email. If the email takes a long time to send, the issue is in your SMTP connection from the app. If it sends in short order but still takes forever to arrive at the recipient, it has to do with the email account(s) or the clients hooked up to them (think the Send / Receive interval in Outlook set too long), but not necessarily your application. That should help you pin the problem down so you know what you are dealing with.

Related