2

ASP.NET Webforms application.

Problem: How to implement emailling elegantly?

Many parts of the application demand it. This will include HTML emails, solutions how to handle bouncebacks etc..

I have the option to use my own SMTP server (app is hosted on a VPS), or to use the hosting providers.

I'd like to start logging here too (log4net probably)

Any suggestions for libraries to use / patterns to implement?

EDIT: http://martinnormark.com/2010/03/13/generate-html-e-mail-body-in-c-using-templates

I like having a debug catch asp.net net.mail - route all emails to a different email address in debug mode

Community
  • 1
  • 1
Dave Mateer
  • 6,588
  • 15
  • 76
  • 125

3 Answers3

2

For testing with a faked out SMTP server use ndumbster. Phil Haack has a blog post on usage - http://haacked.com/archive/2006/05/30/ATestingMailServerForUnitTestingEmailFunctionality.aspx

As you pointed out email templates are fantastic. Consider creating a template for HTML and plain text. Make sure your HTML email templates use basic HTML as a number of email clients out there are not up to modern day browser standards (eg. Outlook which uses the Word HTML rendering engine).

Log4Net is a good point to start at with logging. You can also use System.Diagnostics for simplistic tracing and debugging which can be written easily out to the event log or a file. I always prefer to have a wrapper for all logging so that logging frameworks can easily be swapped out if you find that you want to swap them out later because of a must have feature. It's also good from the point of testability as you can mock it out easily.

Really it doesn't matter which SMTP server you use as long as you don't get black listed. Make sure you check the SMTP servers IP against a DNSBL so you know if your SMTP host has a bad reputation. I highly recommend checking it using Barracuda Central's IP reputation - http://www.barracudacentral.org/lookups. Also make sure you're SMTP server does support resending when you're recipients use grey listing.

For bounce backs you can setup a POP account for it to send back to, it could be as simple as reading the emails and finding which email account the rejection came from and the subject of the previous email (which should be unique) so that you can send again later or after several bounce backs remove them from the list. To implement reading POP take a look at this thread - Reading Email using Pop3 in C#

Community
  • 1
  • 1
Mike737
  • 836
  • 5
  • 16
1

The best pattern you can implement to save yourself loads of trouble down the road is not to send email directly from your web application. Instead, write the email (or the info you need to generate the email) to a database and have a separate process do the mailing.

Consider what happens when your smtp server is down and your app tries to send. If you have a separate process sending email you can send the email later when its back up.

Also you gain the ability to rate limit your outgoing email. If you end up with the Next Big ThingĀ® on your hands you'll be glad you now have the ability to prioritize your outgoing mail or send mailing lists in batches instead of crushing your smtp server at the high point of your site's traffic.

If you are working with bouncebacks just have this process handle that as well. Now you have the ability to log a returned email address to a blacklist and check against the same list for every outgoing email. No way you want to have to put all that in your web app.

Regardless of what else you come up with please consider just writing the email to be sent to a database and letting your app go about its business. You can still get the message going out in near real time but you can get a real good separation of concerns with little effort.

jwsample
  • 2,401
  • 19
  • 18
0

As for incoming messages, I have had great success with the hMailServer which is a free e-mail server for Microsoft Windows. It supports the common e-mail protocols (IMAP, SMTP and POP3) and can easily be integrated with many existing web mail systems. It has flexible score-based spam protection and can attach to your virus scanner to scan all incoming and outgoing email. Definitely worth looking into.

For outgoing emails, I like the patterns I've expressed in this article: .NET MailMessage, LinkedResources, AlternateViews and Exceptions

Ben Griswold
  • 17,793
  • 14
  • 58
  • 60