Windows – How to send mails using a custom .NET application and Windows authentication to a distribution list that only allows authenticated senders

exchange-2007smtpwindowswindows-authentication

This is basically a cross post from my StackOverflow question to see if I can get a server side perspective.

I'm trying to send automated emails from a C# console application from machines to clients all on the same domain via our internal Exchange 2007 server (using SMTP), but I'm hitting a snag with distribution lists that only allow authenticated senders. Basically the mails I'm sending are getting rejected by Exchange with:

#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;AuthTESTGroup@example.com

I'm using System.Net.Mail.SmtpClient and setting the Credentials property to System.Net.CredentialCache.DefaultNetworkCredentials (which should pass through the Windows credentials of the current user running the process), but somewhere along the line, the credentials of the account running this program (me, a valid domain user with a valid mailbox) are not getting passed down to Exchange correctly.

I'm using System.Net.CredentialCache.DefaultNetworkCredentials because I do not want to hard code a username or password (either in the code itself or in any sort of configuration file); I want the process to authenticate with our SMTP server using Windows authentication.

Here is a test program I've been using to reproduce the problem (domain names have been anonomized):

using System;
using System.Net.Mail;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            var smtpClient = new SmtpClient
                {
                    Host = "MAIL",
                    Port = 25,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
                };

            var mailMessage = new MailMessage
                {
                    Body = "Testing",
                    From = new MailAddress(Environment.UserName + "@example.com"),
                    Subject = "Testing",
                    Priority = MailPriority.Normal
                };

            mailMessage.To.Add("AuthTESTGroup@example.com");

            smtpClient.Send(mailMessage);
        }
    }
}

Whenever I run this as myself (again, I'm a valid user on the domain, with an existing mailbox on the Exchange server) I get an undeliverable bounce message from Exchange with the response:

#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;AuthTESTGroup@example.com

I talked to our Exchange server admin and he saw the following error from the Exchange server's event log:

Account For Which Logon Failed:
  Security ID: NULL SID
  Account Name: 
  Account Domain: 

Failure Information:
  Failure Reason: Unknown user name or bad password.
  Status:         0xc000006d
  Sub Status:     0xC0000064

Apparently that status code and sub status code translate to:

0xc000006d This is either due to a bad username or authentication information.  Usually logged as status code with 0xc0000064 as substatus

0xC0000064 user name does not exist

So again, it's as if somewhere along the line, my Windows credentials are not getting passed down to the Exchange server even though I'm setting the SmtpClient.Credentials to System.Net.CredentialCache.DefaultNetworkCredentials

Any ideas?

Or is it possible to somehow configure distribution lists in Exchange 2007 to require authentciated senders except when that sender is on the same domain? (I'm guessing that there's no way for Exchange to guarantee that when mails are sent over SMTP, but I know very little about Exchange administration)

Thanks in advance!

Best Answer

Try setting smtpClient.UseDefaultCredentials = true.

The default value for this property is false. I don't know for sure if that will fix your problem but it seems like an easy thing to try.