C# 从代码发送电子邮件时出现“5.7.1 客户端没有权限”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12081159/
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
'5.7.1 Client does not have permission' error while sending email from code
提问by Kevin DiTraglia
So I have this very basic program that is trying to send an e-mail, but I keep getting
所以我有一个非常基本的程序,它试图发送电子邮件,但我一直收到
Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender
信箱不可用。服务器响应为:5.7.1 客户端无权作为此发件人发送
Here is my program
这是我的程序
static void Main(string[] args)
{
SmtpClient client = new SmtpClient("Server", 25);
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Credentials = new NetworkCredential("UserName", "Password");
client.Send(new MailMessage("[email protected]","Recipient"));
}
I know the credentials work, if I run SMTP Test Tool 3.0 with the same data everything works great.
我知道凭据有效,如果我使用相同的数据运行 SMTP 测试工具 3.0,一切都很好。


Here is some screen shots on a receive connector set up for my IP on the exchange server
这是在交换服务器上为我的 IP 设置的接收连接器的一些屏幕截图




Anybody have any ideas what would be causing this error in my code, but not within the simple SMTP testing tool? Am I missing some kind of authentication option somewhere? I have quadruple checked all the information is correct and identical in both places and it works in the tool but not in the code.
任何人都知道什么会导致我的代码中出现此错误,但在简单的 SMTP 测试工具中却没有?我是否在某处缺少某种身份验证选项?我已经四次检查了两个地方的所有信息是否正确且相同,并且它在工具中有效,但在代码中无效。
采纳答案by Kevin DiTraglia
I found the problem, I needed to check the box 'Accept any sender' for authenticated users.
我发现了问题,我需要为经过身份验证的用户选中“接受任何发件人”框。


More information here: http://technet.microsoft.com/en-us/library/aa997170(EXCHG.140).aspx
更多信息请访问:http: //technet.microsoft.com/en-us/library/aa997170(EXCHG.140).aspx
回答by aleor
I know this thread is quite old, but I just got the same trouble and have been scratching my head for a long time. In my case, mail server didn't accept "foreign" sender, so for example if you are in @sample.com domain, it might be impossible to send mail from "[email protected]", because server will reject this with 5.7.1 error. So, 2 things are important here: 1) Correct credentials which will be used to connect to the server; 2) Value of "From" field, as your server can reject mails from sender that belongs to another domain. In other words, if you are in @sample.com domain, try to add this as well new MailMessage {From = "[email protected]"}.
我知道这个线程已经很老了,但是我遇到了同样的麻烦并且已经挠了很长时间。就我而言,邮件服务器不接受“外国”发件人,因此例如,如果您在@sample.com 域中,则可能无法从“[email protected]”发送邮件,因为服务器将拒绝此操作5.7.1 错误。因此,这里有两件事很重要:1) 将用于连接到服务器的正确凭据;2)“发件人”字段的值,因为您的服务器可以拒绝来自属于另一个域的发件人的邮件。换句话说,如果您在@sample.com 域中,请尝试添加此以及新的 MailMessage {From = "[email protected]"}。
回答by rchacko
I think you have to set UseDefaultCredentials to true: see powershell code
我认为您必须将 UseDefaultCredentials 设置为 true:请参阅 powershell 代码
#SMTP server name
$smtpServer = "abcd.com.au"
#Creating a Mail object
$msg = new-object Net.Mail.MailMessage
#Creating SMTP server object
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.UseDefaultCredentials = $true
回答by Viqas
I had the same problem. I tested the SMTP settings in a separate console application and it worked fine. After some googling, I realised my issue was the fact that I had specified the from address twice, once in my config:
我有同样的问题。我在一个单独的控制台应用程序中测试了 SMTP 设置,它运行良好。经过一番谷歌搜索,我意识到我的问题是我已经两次指定了发件人地址,一次是在我的配置中:
<smtp deliveryMethod="Network" from="[email protected]">
And also in my code:
而且在我的代码中:
mail.From = new MailAddress("[email protected]");
Removing the from address from the code resolved the issue.
从代码中删除 from 地址解决了问题。

