如何使用SmtpClient和DefaultNetworkCredentials将邮件发送到仅允许经过身份验证的发件人的通讯组列表? [英] How to send mails using SmtpClient and DefaultNetworkCredentials to a distribution list that only allows authenticated senders?

查看:617
本文介绍了如何使用SmtpClient和DefaultNetworkCredentials将邮件发送到仅允许经过身份验证的发件人的通讯组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I'm using System.Net.Mail.SmtpClient and setting the Credentials property to System.Net.CredentialCache.DefaultNetworkCredentials, 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.

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

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

每当我以我自己的身份运行时(同样,我是域中的有效用户,并且Exchange服务器上已有邮箱),我会从Exchange收到无法传递的退回邮件,并显示以下响应:

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

我与我们的Exchange服务器管理员进行了交谈,他从Exchange服务器的事件日志中看到以下错误:

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

同样,即使我将SmtpClient.Credentials设置为System.Net.CredentialCache.DefaultNetworkCredentials

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

有什么想法吗?

提前谢谢!

推荐答案

您需要传递用户名,密码

you need to pass username, password

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

here is a code snippet of how I would do it... keep in mind this is a code snippet you need to make the necessary changes to fit your Use Case

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

下面的

是另一个示例,但是使用了server变量.这可能是您需要尝试使我知道的服务器的示例,例如,您可以作为domain.com传递 例子 : //SmtpClient client = new SmtpClient("smtp.contoso.com");//这将是服务器 //client.Credentials = CredentialCache.DefaultNetworkCredentials;

below is another example but uses the server variable.. this maybe what you need to do try and let me know the server for example you can pass as your domain.com example : //SmtpClient client = new SmtpClient("smtp.contoso.com");//this would be server //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());
        }
    }

这篇关于如何使用SmtpClient和DefaultNetworkCredentials将邮件发送到仅允许经过身份验证的发件人的通讯组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆