C# 如何使用 SmtpClient 和 DefaultNetworkCredentials 将邮件发送到只允许经过身份验证的发件人的分发列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9537339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send mails using SmtpClient and DefaultNetworkCredentials to a distribution list that only allows authenticated senders?
提问by russcollier
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:
我正在尝试通过我们的内部 Exchange 2007 服务器(使用 SMTP)将自动电子邮件从 C# 控制台应用程序从机器发送到所有位于同一域中的客户端,但是我遇到了只允许经过身份验证的发件人的分发列表的障碍。基本上我发送的邮件被 Exchange 拒绝了:
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;[email protected]
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;[email protected]
I'm using System.Net.Mail.SmtpClientand 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.Mail.SmtpClientCredentials 属性并将其设置为System.Net.CredentialCache.DefaultNetworkCredentials,但在此过程中,运行此程序的帐户的凭据(我,具有有效邮箱的有效域用户)没有正确传递给 Exchange。
I'm using System.Net.CredentialCache.DefaultNetworkCredentialsbecause 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.
我使用System.Net.CredentialCache.DefaultNetworkCredentials是因为我不想硬编码用户名或密码(在代码本身或任何类型的配置文件中);我希望该进程使用 Windows 身份验证与我们的 SMTP 服务器进行身份验证。
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("[email protected]");
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:
每当我以自己的身份运行此程序时(同样,我是域中的有效用户,并且在 Exchange 服务器上有一个现有邮箱),我会收到来自 Exchange 的无法投递的退回邮件,并给出以下响应:
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;[email protected]
#550 5.7.1 RESOLVER.RST.AuthRequired; authentication required ##rfc822;[email protected]
I talked to our Exchange server admin and he saw the following error from the Exchange server's event log:
我与我们的 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
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
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
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.Credentialsto System.Net.CredentialCache.DefaultNetworkCredentials
再说一次,就好像在某个地方,即使我将 Windows 凭据设置SmtpClient.Credentials为System.Net.CredentialCache.DefaultNetworkCredentials
Any ideas?
有任何想法吗?
Thanks in advance!
提前致谢!
回答by MethodMan
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);
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;
下面是另一个示例,但使用了服务器变量。 com");//这将是服务器 //client.Credentials = CredentialCache.DefaultNetworkCredentials;
public static void CreateBccTestMessage(string server)
{
MailAddress from = new MailAddress("[email protected]", "Ben Miller");
MailAddress to = new MailAddress("[email protected]", "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("[email protected]");
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());
}
}

