如何使用SmtpClient和DefaultNetworkCredentials将邮件发送到仅允许经过身份验证的发件人的通讯组列表?

时间:2012-03-02 17:03:12

标签: c# windows-authentication smtpclient exchange-server-2007

我正在尝试通过我们的内部Exchange 2007服务器(使用SMTP)将C#控制台应用程序中的自动电子邮件从计算机发送到同一域中的所有客户端,但我遇到了只允许通过身份验证的通讯组列表的问题发件人。基本上我发送的邮件被Exchange拒绝了:

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

我正在使用System.Net.Mail.SmtpClient并将凭据属性设置为System.Net.CredentialCache.DefaultNetworkCredentials,但是在该行的某处,运行此程序的帐户的凭据(我,具有有效邮箱的有效域用户)没有正确传递给Exchange。

我正在使用System.Net.CredentialCache.DefaultNetworkCredentials,因为我不想硬编码用户名或密码(在代码本身或任何类型的配置文件中);我希望该过程使用Windows身份验证对我们的SMTP服务器进行身份验证。

以下是我用来重现问题的测试程序(域名已经过网络化):

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);
        }
    }
}

每当我以自己的身份运行时(同样,我是域中的有效用户,Exchange服务器上有现有邮箱)我从Exchange收到无法投递的退回邮件:

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

我和我们的Exchange服务器管理员谈过,他从Exchange服务器的事件日志中看到以下错误:

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

显然,状态代码和子状态代码转换为:

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

所以再一次,就好像在某个地方,即使我将SmtpClient.Credentials设置为System.Net.CredentialCache.DefaultNetworkCredentials

,我的Windows凭据也不会传递到Exchange服务器

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:3)

您需要传递用户名,密码

这是我将如何操作的代码片段...请记住,这是一个代码片段,您需要进行必要的更改以适合您的用例

MailClient = new SmtpClient();
MailClient.Credentials = new System.Net.NetworkCredential(username, password);

下面是另一个例子,但使用服务器变量..这可能是你需要做的尝试让我知道服务器,例如你可以作为你的domain.com传递 例如:     // SmtpClient客户端=新的SmtpClient(“smtp.contoso.com”); //这将是服务器     //client.Credentials = CredentialCache.DefaultNetworkCredentials;

    public static void CreateBccTestMessage(string server)
    {
        MailAddress from = new MailAddress("ben@contoso.com", "Ben Miller");
        MailAddress to = new MailAddress("jane@contoso.com", "Jane Clayton");
        MailMessage message = new MailMessage(from, to);
        message.Subject = "Using the SmtpClient class.";
        message.Body = @"Using this feature, you can send an e-mail message from an application very easily.";
        MailAddress bcc = new MailAddress("manager1@contoso.com");
        message.Bcc.Add(bcc);
        SmtpClient client = new SmtpClient(server);
        client.Credentials = CredentialCache.DefaultNetworkCredentials;
        Console.WriteLine("Sending an e-mail message to {0} and {1}.",
            to.DisplayName, message.Bcc.ToString());
        try
        {
            client.Send(message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception caught in CreateBccTestMessage(): {0}",
                        ex.ToString());
        }
    }