C#/.NET 的电子邮件发送策略
我有一个网络应用程序,在执行特定操作后应从该应用程序发送电子邮件。我有一些替代方案来处理这个问题,但我不确定哪一个是最好的。
第一个是,当用户执行操作时,电子邮件将直接从 ASP.NET 应用程序发送。但我认为这不是一个真正可靠的系统,因为如果 SMTP 服务器发生故障或发生其他情况,用户只会收到他的操作无法完成的反馈。
作为替代方案,我正在考虑实现一个排队系统,以实现我的一些想法:
- 将要发送的电子邮件推送到数据库表中,服务应用程序定期检查新消息,然后发送它们。成功发送后,标志着电子邮件任务已完成。
- 使用 MSMQ 进行排队。在这种情况下,整个电子邮件可以作为消息传递;或者另一种方法是将带有附件的消息存储到数据库表中,并仅传递查询数据库表和发送消息所需的数据。在这种情况下,我不必处理 MSMQ 的大小限制(因为附件)。
- 其他的东西,比如本地 WCF 服务来通知服务
您认为哪种方式最好?
I have a web application from which emails should be sent after specific actions. I have some alternatives for handling this I'm not sure which one is the best.
The first is that, when a user does an action the email is being sent directly from the ASP.NET application. But I think this is not a really reliable system because if the SMTP server is down or something else happens, the user just gets a feedback that his action cannot be completed.
As an alternative I was thinking about implementing a queuing system for what I have some ideas:
- Push emails to send, into a database table, and a service application periodically checks for new messages, then sends them. On successful send it marks the email task completed.
- Use MSMQ for queing. In this case the whole email could be passed as a message; or the other way is to store the message with attachments into a db table, and pass only the data which is required to query the db table and send the message. In this case I don't have to deal with size limits of MSMQ (because of attachments).
- something else, like a local WCF service to notify the service
Which way you think is the best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 MSMQ 不是一个好的解决方案,因为每个大小有 4 MB 的限制。 http://blogs.msdn.com/b/johnbreakwell/archive/2007/08/22/why-is-there-a-4mb-limit-on-msmq-messages.aspx
最糟糕的情况是,如果 MSMQ 失败,例如进程抛出错误或突然关闭,它将丢失许多消息。就我而言,当硬件和软件安装得几乎理想时,此解决方案很好。
使用数据库和窗口服务更好,因为它很简单,不需要太多努力。
我通常使用数据库和文件的组合。数据库包含用于保存标头信息和消息已执行操作的标志(成功或错误或其他)的表,文件包含原始格式的消息(html 或纯文本)和附件。
当运行进程发送时,从文件中组装消息比从查询 blob/clob 中组装消息更快。
由于它们在应用程序上使用文件系统,因此您可以添加服务器或组件等硬件,或者轻松地添加系统的可用性。
也可以添加数据库,但这会花费您更多的数据库软件许可证。
我在发送电子邮件 x 次后添加测试发送电子邮件,以确保其正常工作;该测试电子邮件将发送到我自己或虚拟收件箱以及一个用于检查测试电子邮件的应用程序,该测试电子邮件与发送和接收的电子邮件相同。如果相同,将再次继续发送待处理的电子邮件。
如果您使用 MS Exchange,您可以通过利用其 Web 服务对发送进行排队来使用消息队列。这是一种简单的方法,但您需要许可证。
您可以在 MSDN 库上查看如何利用 MS Exchange Web 服务。
Use MSMQ is not good solution since has a limitation of 4 MB of each size. http://blogs.msdn.com/b/johnbreakwell/archive/2007/08/22/why-is-there-a-4mb-limit-on-msmq-messages.aspx
Worse case scenario, if MSMQ is failed like it process throw error or suddenly shutdown, it will loss many message. In my case, this solution is good when hardware and software is instaled in almost ideal
Use database and window service is better since it is a simple and doesn't need much effort.
I usually use a combination of database and file. The database contains table to save a header information and a flag that message has been action (either success or error or else) and files contains message (either html or plain) and attachment in original format.
When process is run to send, it is quicker to assemble a message from files rather than from querying blob/clob.
Since they are using file system on the application, you can add hardware like server or components or else to add availibility of the system easily.
Database can be added too, but it will cost you more license in databse software.
I add a test send email after send email in x times to make sure it is works well; this test email is send to my self or dummy inbox and an application to check the test email that is the same email that send and receive. If it is the same, sending pending email will continue again
Another way if you are using MS Exchange, you can use message queue by utilize its web service to queue send. This is an easy way but you need license.
You can see on MSDN library how to utilize MS Exchange web service.
您可以使用 hmail 等电子邮件服务器。为了将电子邮件推送到队列中,您可以将它们推送到邮件服务器。为此,您可以编写一个 Windows 窗体应用程序,该应用程序具有一个计时器对象,该对象检查电子邮件表中状态为 0(未发送)的每一行。当线程将其发送到邮件服务器时,它会被标记为1(已发送)。
如果您使用 DB,您还可以对电子邮件进行分类。不同的操作可以发送不同的电子邮件。您也可以将此信息存储在数据库中,以便您的 Windows 窗体应用程序线程现在可以发送哪个电子邮件模板。
You can use an email server like hmail. In order to push emails into a queue, you can push them to a mail server. To do that, you can write a windows form application that has a timer object that checks every row that has a Status 0(not sent) in email table. When the thread sends it to the mail server, it will be marked as 1(sent).
You can also classify your emails if you use DB. Different actions can send different emails. You can store this info in DB also so that your windows form application thread will now which email template to send.