ASP.NET MVC: Send email using SendAsync (System.Net.Mail)

血红的双手。 提交于 2019-12-05 00:43:25

问题


is there any way in MVC to get the System.Net.Mail SendAsync to work, instead of the blocking Send method?

I tried using it but hit the "Page starting an asynchronous operation has to have the Async attribute" error, which I obviously can't resolve (or can I?) because there is no ASPX page with an @Page directive where I could add the Async attribute.

Help is greatly appreciated :(


回答1:


It looks like you want Asynchronous support for ASP.NET MVC. See also "Extend ASP.NET MVC for Asynchronous Action".

This SO question is also relevant.




回答2:


If you're using a third party service such as gmail to send your email and relying on SendEmail or SendAsync you may want to set up your own SMTP server which will then relay onto gmail. The benefit is that SendEmail will return much faster and you may be able to forget the whole async thing.

That way if gmail rejects you for whatever reason the email will still sit in your local queue.

This is a quick and dirty way to get somewhat reliable email sending setup.

Instructions (not personally tested, but i did this years ago*) or see this for Windows 7

*of course I only remembered i did this after spending a long time converting over to async :-(




回答3:


You may want to consider putting the email into a database table and then have a daemon that sends out emails by processing the queue.

a) if youre not sending billions of emails you can have a log of what you sent
b) you can resend if something fails.

depends how important these emails are, but if theyre important from a business standpoint and mustnt get lost then you should use this approach.



来源:https://stackoverflow.com/questions/931385/asp-net-mvc-send-email-using-sendasync-system-net-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!