开发者

Email sending strategy with C#/.NET

开发者 https://www.devze.com 2023-04-12 03:25 出处:网络
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.

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 w开发者_如何学运维ay you think is the best?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消