views:

68

answers:

3

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 http://stackoverflow.com/questions/638768/asp-net-net-mail-route-all-emails-to-a-different-email-address-in-debug-mode

A: 

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
A: 
jwsample
+1  A: 

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 - http://stackoverflow.com/questions/44383/reading-email-using-pop3-in-c

Mike737
Many thanks Mike737, jwsample and Ben.
Dave