Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extremely Slow SmtpClient Using Office 365

I'm working on MVC5 website. This website contains "contact us" form where user can contact website support.

The problem is that the SendMailAsync takes at least 10-13 seconds to complete (no attachments) and while this process is running, user is stuck and waiting for the response from the server - whether the email was sent successfully or not.

This is my current ActionResult method:

[HttpPost]
public async Task<ActionResult> Contact(MessageModel Model)
{
    //...
    await SendMessage(Model);
    //...
    return View();
}

And this is my method responsible for sending the email:

private async Task SendMessage(MessageModel Model)
{
    var Message = new MailMessage();
    Message.From = ...;
    Message.To.Add(...);
    Message.Subject = ...;
    Message.IsBodyHtml = true;

    SmtpClient Client = new SmtpClient();
    Client.UseDefaultCredentials = false;
    Client.Credentials = ...;
    Client.Port = 587;
    Client.Host = "smtp.office365.com";
    Client.DeliveryMethod = SmtpDeliveryMethod.Network;
    Client.EnableSsl = true;

    await Client.SendMailAsync(Message);
}

Are there any faster working alternatives? I could run it like this:

await Task.Factory.StartNew(() => SendMessage(Model));

The website would not be "stuck" for 13 seconds and response would be instantaneous, however, I would not be able too provide the user with information whether the email was sent successfully or not.

like image 505
OverflowStack Avatar asked Nov 08 '22 02:11

OverflowStack


1 Answers

We ran into similar problems recently for an event registration, 4-5 sec for each email, and we needed to send multiple with different content. The client's IT person was able to get a .protection.outlook.com:25 server accepting non ssl traffic running on the same domain and it took the time to 1-1.5 sec. Which was fast enough for the client with some UI to show we were processing. We also tried a local server and it was only about 100ms, but they didn't want to have that email service running on their DC.

I can't speak to the specifics of the setup for this .protection.outlook.com server since I wasn't involved, but it may be something to look into if you don't want to rely on async.

like image 82
CJ Fravel Avatar answered Nov 14 '22 21:11

CJ Fravel