Remove Recipients From Email When Not In Release Mode

Often, we need our applications to send emails to people.  When this is the case, if we are not careful, we can end up sending test emails to our external users.  We can get around this very easily by using preprocessor directives and writing our own SmtpClient class.

This isn’t a tutorial on sending email through .net, but here is a very basic function that will do that.  First the usings:

using System.Collections.Generic;
using System.Net.Mail;

Then the function:

public void sendEmail(string message, string subject, MailAddress fromAddress, 
            List<MailAddress> toAddressList, List<MailAddress> ccAddressList, 
            List<MailAddress> bccAddressList)
{
    MailMessage mail = new MailMessage();
    SmtpClient client = new SmtpClient();

    mail.From = fromAddress;
    mail.Subject = subject;
    mail.Body = message;

    foreach (MailAddress address in toAddressList)
    {
        mail.To.Add(address);
    }

    foreach (MailAddress address in ccAddressList)
    {
        mail.CC.Add(address);
    }

    foreach (MailAddress address in bccAddressList)
    {
        mail.Bcc.Add(address);
    }

    client.Host = "smtp.gmail.com";
    client.Port = 587;
    client.Credentials = new System.Net.NetworkCredential("username", "****");
    client.EnableSsl = true;

    client.Send(mail);
}

Now, that is just a generic function that will send an email.  I’m not going to bother explaining it.  See that last line though: “client.Send(mail);”?  That Send function is what we want to change.  To do that, we create a new class, with SmtpClient as the base, and then overload the Send function.

Our usings are the same:

using System.Collections.Generic;
using System.Net.Mail;

And here is our new class:

class NewSmtpClient : SmtpClient
{
    public void Send(MailMessage message)
    {
#if DEBUG
        List<MailAddress> toList = new List<MailAddress>(message.To);
        List<MailAddress> ccList = new List<MailAddress>(message.CC);
        List<MailAddress> bccList = new List<MailAddress>(message.Bcc);

        foreach(MailAddress address in toList)
        {
            if(address.Host.ToLower() != "gmail.com")
            {
                message.To.Remove(address);
            }
        }

        foreach (MailAddress address in ccList)
        {
            if (address.Host.ToLower() != "gmail.com")
            {
                message.CC.Remove(address);
            }
        }

        foreach (MailAddress address in bccList)
        {
            if (address.Host.ToLower() != "gmail.com")
            {
                message.Bcc.Remove(address);
            }
        }
#endif

        //there must be at least one recipient 
        if ((message.To.Count + message.CC.Count + message.Bcc.Count) > 0)
        {
            base.Send(message);
        }
    }
}

Of course, now that we have our new class, we need to change our calling function.  So, in our generic function we will change

SmtpClient client = new SmtpClient();

to

NewSmtpClient client = new NewSmtpClient();

So, I want to explain a few things here.  See that fifth line, #if DEBUG?  That’s a preprocessor directive.  Visual Basic has them to, but MSDN just called them directives.  Anyway, these directives are processed before the compiler runs.  Anything between #if DEBUG and #endif will not even show up in the IL Code if you compile in Release mode.

What is happening in our example class here, is that when you call client.Send(mail), if you are in DEBUG mode, then we remove any email recipient whose host is not “gmail.com”.  Of course, you would use any host that you want.  This results in a recipient list that is any of the original recipients but, no emails will be sent to “gmail.com”.  I don’t think that is worded very well, so let me visualize it for you.

  • To List
    • one@gmail.com
    • one@yahoo.com
    • one@bing.com
  • CC List
    • two@gmail.com
    • two@yahoo.com
    • two@bing.com
  • BCC List
    • three@gmail.com
    • three@yahoo.com
    • three@bing.com

After we call Send, but before the email is actually sent, the new recipient list looks like this:

  • To List
    • one@gmail.com
  • CC List
    • two@gmail.com
  • BCC List
    • three@gmail.com

The if statement at the end is because it is possible that we will end up removing all of our recipients, in which case we don’t want to send the email.  In this scenario, but without such an if statement, an exception will be thrown.

If this post has helped you in any way, please leave a comment below. Or, if you solved this method another way, please leave a comment.  I would like to hear from you!

Advertisements