ASP.NET MVC:使用SendAsync发送电子邮件(System.Net.Mail)

时间:2021-08-28 18:14:30

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

在MVC中是否有任何方法可以使System.Net.Mail SendAsync工作,而不是阻塞Send方法?

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.

我尝试使用它,但点击“启动异步操作的页面必须有Async属性”错误,我显然无法解决(或者我可以?)因为没有带有@Page指令的ASPX页面我可以添加Async属性。

Help is greatly appreciated :(

非常感谢帮助:(

3 个解决方案

#1


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

看起来您需要ASP.NET MVC的异步支持。另请参阅“为异步操作扩展ASP.NET MVC”。

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.

如果您使用第三方服务(如gmail)发送电子邮件并依赖SendEmail或SendAsync,您可能需要设置自己的SMTP服务器,然后将其转发到gmail。好处是SendEmail返回的速度要快得多,你可能会忘记整个异步的事情。

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

这样,如果gmail因任何原因拒绝您,那么该电子邮件仍会保留在您的本地队列中。

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

说明(未经过个人测试,但我多年前做过*)或者看到这个用于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.

取决于这些电子邮件的重要程度,但如果从商业角度来看它们很重要并且不会丢失,那么您应该使用这种方法。

#1


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

看起来您需要ASP.NET MVC的异步支持。另请参阅“为异步操作扩展ASP.NET MVC”。

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.

如果您使用第三方服务(如gmail)发送电子邮件并依赖SendEmail或SendAsync,您可能需要设置自己的SMTP服务器,然后将其转发到gmail。好处是SendEmail返回的速度要快得多,你可能会忘记整个异步的事情。

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

这样,如果gmail因任何原因拒绝您,那么该电子邮件仍会保留在您的本地队列中。

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

说明(未经过个人测试,但我多年前做过*)或者看到这个用于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.

取决于这些电子邮件的重要程度,但如果从商业角度来看它们很重要并且不会丢失,那么您应该使用这种方法。