简体   繁体   中英

Sending email from ASP.NET MVC

I'm writing a user registration process in ASP.NET MVC 3 RC, and I'd like to send a confirmation email to the user during that process. I know I can do this using the classes in the System.Net.Mail namespace, but I have a few questions on the best way to implement this.

  1. Before I go down the path of writing my own code to send email, are there any existing solutions like ActionMailer for ASP.NET MVC?

  2. If I do roll my own, should I do this on the web server during the request and would using Send bog down the web server, tying up threads as they wait on the SMTP server to respond? If so, would SendAsync be preferable?

  3. Would it be better pull this out of ASP.NET MVC entirely, maybe by writing a record to a database table and having a windows service periodically sweep the table and send out the messages?

At the moment, I am leaning towards #3, since that would give me the flexibility of doing this work on another server entirely, but was wondering what others are doing. Are there options and/or issues I haven't considered?

Personally I would agree that #3 is your best option. It's the most complex of the three, but none of them are terribly complex. What it does do, however, is:

  1. Give you a record of all emails sent by the system.
  2. Remove the dependency of the email system from the live application. (Separation of concerns is always fun.)
  3. Provide a natural re-try mechanism when the email system dependency is failing.

As for an existing library for making the creation/sending of the MailMessage objects easier, I've never seen a need. If you're just sending the messages to an SMTP server, the built-in objects are more than adequate and easy to use.

MvcMailer is like an ActionMailer for .Net. See the NuGet package here and the project documentation

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM