Monday, April 25, 2011 net.mail - route all emails to a different email address in debug mode

Is there anyway to have all emails go to a different address when System.Web.HttpContext.Current.IsDebuggingEnabled is true? Or do I need a new class which I use to send email everywhere?

From stackoverflow
  • You could either put this in your code everywhere you use it:

    if (System.Web.HttpContext.Current.IsDebuggingEnabled)
     mail.To = "";
     mail.To = recipientAddress;

    Or just build a simple wrapper for the getting of email addresses (even something in the getter of the method for the recipient addresses would work) or a wrapper for the whole smtpclient or mailmessage class.

  • The best way to handle this, I think, is via a proxy class for the mail client. When you create the proxy class use a parameter to indicate whether to operate in debug mode or not. Have the proxy support the same interface as the mail client, but silently replace the outgoing email addresses when it is operating in debug mode.

    public class MailProxy
         private bool IsDebug { get; set; }
         private string DebugAddress { get; set; }
         private SmtpClient Client { get; set; }
         public MailProxy( SmtpClient client,
                           bool debugging,
                           string debugAddress )
             this.IsDebug = debugging;
             this.DebugAddress = debugAddress;
         public void Send( MailMessage message )
             if (this.IsDebug)
                 message.To = new MailAddress(this.DebugAddress);
             this.Client.Send( message );
  • I'd set up an SMTP server and have your webapp use that as its outgoing mail server (specified in some kind of config). For production sites, the server would behave normally, but for debugging, you can configure an SMTP server to route all mail to your address of choice. This has the advantage of not changing your app at all, which can be useful if changing the behavior prevents bugs from occuring that you're trying to reproduce, etc. My company uses this approach for QA and it works quite well.

    Shawn Simon : i was considering this but i dont know how well it would work in shared production environments
    rmeador : By "shared production", do you mean you're testing on your production system? If so, that's a major no-no. Accidentally spamming your customers is not going to win you any points. If you mean you're sharing the test server with other developers, we solve that by having a shared test mailbox.
  • I have to be honest I use good old

    #if DEBUG

    To stop me having debug code floating around in my production apps. An approach I use for database access is to use a dns entry, i.e. and in the hosts file on my dev machine it points to my local system and on my production machine it points to the database server. You could use something similar to have a debug mail server and a production mail server but it seems overkill compared to the first solution above.

  • We use a similar solution to the MailProxy method detailed by tvanfosson. However we have a few differences that I feel are worth mentioning. The first is that we simply extend the SmtpClient class and override the Send methods when in a development or testing environment. We use a config file to determine the environment. We then use this class anytime we need to send out emails. This class will determine who is currently logged into the application and redirect all emails generated to them, with a failover to a standard list of recipients if the logged in user is not available for whatever reason. This helps if you have several people who are testing or developing as they will only get the emails they generated. We also prepend the original recipients of the message to the body of the message. This again is useful when testing workflow type messages to ensure that they are going to the correct individuals. Finally, I find it's useful to add a [DEV] or [QA] tag to the subject of the message so I can set up a rule in my mail client to handle all testing messages.

    tvanfosson : I thought about this but the methods are marked virtual which could give you problems if the object is cast as an SmtpClient.
    Shawn Simon : i just used shadows


Post a Comment