Quick Tip: Sending e-Mail to a Distribution list via C# in Exchange 2007 / 2010…

I have been doing quite a bit of work with C# recently working on the new version of the OOOCTA tool.

One of the features within the OOOCTA is for members of an Exchange distribution list to receive an e-mail when the OOF has been set on a particular mailbox.
Now the most practical way to make use of Exchange Distributions list in Exchange is to use the:

System.Net.Mail.MailMessage

Message class – which allows for your C# application to send e-mails via SMTP to the e-mail address that is assigned to the Distribution list.

So an example function might look like:

public void sendMail(string subject,string bodyParams)
       {
           
           string SMTPHost = “172.31.253.x”;
           string fromAddress = [email protected]”;
           string toAddress = [email protected]”;
           System.Net.Mail.MailMessage notMess = new System.Net.Mail.MailMessage();
           notMess.To.Add(toAddress);
           notMess.Subject = subject;
           notMess.From = new System.Net.Mail.MailAddress(fromAddress);
           notMess.Body = bodyParams;
           System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(SMTPHost);
           smtp.Send(notMess);
       }

In the function above you can see that I assign the destination SMTP address to string toAddress and then construct the message, and then relay that to the SMTP server which is described in the variable string SMTPHost.

However it is important to note that the function above will not work in a default configuration of Exchange 2007 and Exchange 2010 for two key reasons:

  • The function does not provide any mechanism for authenticating your application to the SMTP server (if you are using an Exchange Receive connector as you SMTP smart host it in it’s default configuration expect that any attempt to relay through will be authenticated)
  • When a distribution group is created in Exchange 2007 and Exchange 2010 there is a default setting applied to it called “RequireSenderAuthenticationEnabled” which will be set to “True”, very much like point 1 above; the Distribution Group will expect any attempt to send mail to it to be authenticated

If you do not wish to implement Authentication within your application, there are two workarounds around the above situation:

  • Configure a new SMTP receive connector (with its own IP address) on your Exchange HT server which allows for Anonymous Relay (do not do this under any circumstances on a HT or Edge which faces the Internet) and configure the connector to only accept connections from the IP range or Server that your application is resident on – to create the connector see: http://technet.microsoft.com/en-us/library/bb232021(EXCHG.80).aspx
  • Use the following Powershell command to turn off the Authentication requirement for your Applications Distribution group:
[PS] C:\>Set-DistributionGroup –id -RequireSenderAuthenticationEnabled $false

Of course the best way is to ensure that you implement authentication into the Application when communicating with SMTP servers – as the workarounds above decrease your security posture – but for simple applications in non-critical environments the above can be used to achieve the desired results from your code.

Sharing is caring!:

One thought to “Quick Tip: Sending e-Mail to a Distribution list via C# in Exchange 2007 / 2010…”

  1. What happens when one of the members fo the DL is an invalid address. In my case the SMTP server reject the entire message as being undeliverable. Any ideas ?

Leave a Reply

Your email address will not be published. Required fields are marked *