I'm working on MVC5 website. This website contains "contact us" form where user can contact website support.
我正在研究MVC5网站。本网站包含“联系我们”表格,用户可以联系网站支持。
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.
问题是SendMailAsync至少需要10-13秒才能完成(没有附件),当此过程正在运行时,用户会卡住并等待来自服务器的响应 - 无论电子邮件是否成功发送。
This is my current ActionResult method:
这是我当前的ActionResult方法:
[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.
该网站不会被“卡住”13秒并且响应将是即时的,但是,我无法向用户提供有关电子邮件是否成功发送的信息。
1 个解决方案
#1
0
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.
我们最近遇到了类似的问题,每个电子邮件发送一次事件,每个电子邮件需要4-5秒,我们需要发送多个不同的内容。客户的IT人员能够获得.protection.outlook.com:25服务器接受在同一域上运行的非ssl流量,并且花费的时间为1-1.5秒。这对于客户端而言足够快,其中一些UI显示我们正在处理。我们还尝试了本地服务器,它只有大约100毫秒,但他们不希望在他们的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.
我不能谈论这个.protection.outlook.com服务器的设置细节,因为我没有参与,但如果你不想依赖异步,可能需要考虑一下。
#1
0
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.
我们最近遇到了类似的问题,每个电子邮件发送一次事件,每个电子邮件需要4-5秒,我们需要发送多个不同的内容。客户的IT人员能够获得.protection.outlook.com:25服务器接受在同一域上运行的非ssl流量,并且花费的时间为1-1.5秒。这对于客户端而言足够快,其中一些UI显示我们正在处理。我们还尝试了本地服务器,它只有大约100毫秒,但他们不希望在他们的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.
我不能谈论这个.protection.outlook.com服务器的设置细节,因为我没有参与,但如果你不想依赖异步,可能需要考虑一下。