.NET SMTP客户端 - 客户端无权作为此发件人发送

时间:2021-08-24 15:17:02

I'm getting strange occurance on our servers when I am trying to send an email using SmtpClient class via an ASP MVC3 project. This is the code I am using.

当我尝试通过ASP MVC3项目使用SmtpClient类发送电子邮件时,我的服务器上出现了奇怪的现象。这是我正在使用的代码。

try
{
    var client = new SmtpClient("MailServer");
    client.UseDefaultCredentials = true;

    MailMessage message = new MailMessage("me@mydomain.com", "friend@mydomain.com", "Test Message", "Test Body");
    client.Send(message);
}
catch (Exception ex)
{
    // Do Nothing
}

I have deployed on three environments; on Windows 7 (using VS 2010 IIS) it sends the email fine, on the Windows 2003 IIS6 machine it sends the email fine, finally on the Windows 2008 R2 II7 server I get the following error:

我已经部署在三个环境中;在Windows 7上(使用VS 2010 IIS)它发送电子邮件很好,在Windows 2003 IIS6机器上它发送电子邮件很好,最后在Windows 2008 R2 II7服务器上我收到以下错误:

Mailbox unavailable. The server response was: 5.7.1 Client does not have permissions to send as this sender using username 

Can anybody advise on what may be causing this. I have noticed that when I view User.Identity.Name, this is returning an empty string.

任何人都可以就可能造成这种情况的原因提出建议。我注意到当我查看User.Identity.Name时,这将返回一个空字符串。

8 个解决方案

#1


15  

It is likely that the mail server does not support sending anonymous emails and will require credentials to be specified which are registered on the mail server.

邮件服务器可能不支持发送匿名电子邮件,并且需要指定在邮件服务器上注册的凭据。

You can specify the credentials like so:

您可以像这样指定凭据:

client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("username", "password");

Hope this helps.

希望这可以帮助。

#2


6  

If you are using an exchange server and the logon account and sender email are different you will got the error "does not have permissions to send...". This is because of the account permissions. You must grant "send as" permission to the logon account.

如果您使用的是Exchange服务器且登录帐户和发件人电子邮件不同,则会收到错误“无权发送...”。这是因为帐户权限。您必须向登录帐户授予“发送为”权限。

#3


5  

I solve this problem removing authentication credentials from web.config

我解决了从web.config中删除身份验证凭据的问题

<system.net>
<mailSettings>
  <smtp from="SystemAdmin@domain.do">
    <!--network host="EXCH-SERVER" port="25" userName="userName" password="password" defaultCredentials="false" /-->
    <network host="EXCH-SERVER" port="25" />
  </smtp>
</mailSettings>

#4


1  

If you have try everything and it is still failing, one possibility is that the server accept only anonymous user. If you try to connect with credentials:

如果你已经尝试了所有东西并且它仍然失败,那么一种可能性是服务器只接受匿名用户。如果您尝试连接凭据:

  • defaultCredentials="true"
  • 的DefaultCredentials = “真”
  • defaultCredentials="false" userName="foo" password="false"
  • defaultCredentials =“false”userName =“foo”password =“false”

Then the server WILL return the error .NET SMTP Client - Client does not have permissions to send as this sender.

然后服务器将返回错误.NET SMTP客户端 - 客户端没有作为此发件人发送的权限。

That's weird, but as a last resort, just try the simple:

这很奇怪,但作为最后的手段,只需尝试简单:

  • defaultCredentials="false"
  • 的DefaultCredentials = “假”

#5


0  

What does User.Identity.IsAuthenticated return, if it returns false that is your problem. You are trying to send mail as an unauthenticated user.

User.Identity.IsAuthenticated返回什么,如果它返回false就是你的问题。您正在尝试以未经身份验证的用户身份发送邮件。

#6


0  

Had the same issue - the credentials from ASP.Net were never going to be something that could send e-mail in my environment. So I figured out this path through the mess (which also includes the possibility that NTLM doesn't always work right and that I was putting the mail configuration info in web.config):

有同样的问题 - 来自ASP.Net的凭证永远不会成为可以在我的环境中发送电子邮件的东西。所以我在混乱中找到了这条路径(其中还包括NTLM并不总是正常工作的可能性以及我将邮件配置信息放在web.config中):

System.Net.Configuration.SmtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;

// set up SMTP client
SmtpClient smtp = new SmtpClient();
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
NetworkCredential myCred = new NetworkCredential(section.Network.UserName, section.Network.Password);
string host = section.Network.Host;
int port = section.Network.Port;

// do this because NTLM doesn't work in all environments....
if (myCred != null)
{
    myCache.Add(host, port, "Digest", myCred);
    myCache.Add(host, port, "Basic", myCred);
    myCache.Add(host, port, "Digest-MD5", myCred);
    myCache.Add(host, port, "Digest MD5", myCred);
    myCache.Add(host, port, "Plain", myCred);
    myCache.Add(host, port, "Cram-MD5", myCred);
    myCache.Add(host, port, "Cram MD5", myCred);
    myCache.Add(host, port, "Login", myCred);

    //myCache.Add(host, port, "NTLM", myCred);
}
smtp.Credentials = myCache;
smtp.UseDefaultCredentials = false;
//smtp.EnableSsl = true;

Depending on your configuration, you might need to uncomment the last line.

根据您的配置,您可能需要取消注释最后一行。

#7


0  

For me it was using different credentials -

对我来说,它使用不同的凭据 -

In network credentials I was using -

在我使用的网络凭证中 -

new NetworkCredential(smtpUser, smtpPassword)

and in fromAddress

并在fromAddress中

 var fromAddress = new MailAddress("anotheruser@gmail.com", string.Empty);

Where when sending email where fromAddress is different than actual network credentials will lead this issue. See below -

在发送电子邮件时,fromAddress与实际网络凭据不同将导致此问题。见下文 -

 using (var message = new System.Net.Mail.MailMessage(fromAddress, toAddress)
                {
                    Subject = Keys.MailSubject,
                    Body = body,
                    IsBodyHtml = true
                })
                {
                    smtp.Send(message);

                }

The simple fix is to keep both the mails same as network credentials's one.

简单的解决方法是将两个邮件保持为网络凭证的一个。

#8


0  

One thing worked for me using Visual studio 2013 running packages. i removed the 'Use Windows Authentication' tag in the SMTP Connection Manager Editor.

使用Visual Studio 2013运行包有一件事对我有用。我删除了SMTP连接管理器编辑器中的“使用Windows身份验证”标记。

#1


15  

It is likely that the mail server does not support sending anonymous emails and will require credentials to be specified which are registered on the mail server.

邮件服务器可能不支持发送匿名电子邮件,并且需要指定在邮件服务器上注册的凭据。

You can specify the credentials like so:

您可以像这样指定凭据:

client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("username", "password");

Hope this helps.

希望这可以帮助。

#2


6  

If you are using an exchange server and the logon account and sender email are different you will got the error "does not have permissions to send...". This is because of the account permissions. You must grant "send as" permission to the logon account.

如果您使用的是Exchange服务器且登录帐户和发件人电子邮件不同,则会收到错误“无权发送...”。这是因为帐户权限。您必须向登录帐户授予“发送为”权限。

#3


5  

I solve this problem removing authentication credentials from web.config

我解决了从web.config中删除身份验证凭据的问题

<system.net>
<mailSettings>
  <smtp from="SystemAdmin@domain.do">
    <!--network host="EXCH-SERVER" port="25" userName="userName" password="password" defaultCredentials="false" /-->
    <network host="EXCH-SERVER" port="25" />
  </smtp>
</mailSettings>

#4


1  

If you have try everything and it is still failing, one possibility is that the server accept only anonymous user. If you try to connect with credentials:

如果你已经尝试了所有东西并且它仍然失败,那么一种可能性是服务器只接受匿名用户。如果您尝试连接凭据:

  • defaultCredentials="true"
  • 的DefaultCredentials = “真”
  • defaultCredentials="false" userName="foo" password="false"
  • defaultCredentials =“false”userName =“foo”password =“false”

Then the server WILL return the error .NET SMTP Client - Client does not have permissions to send as this sender.

然后服务器将返回错误.NET SMTP客户端 - 客户端没有作为此发件人发送的权限。

That's weird, but as a last resort, just try the simple:

这很奇怪,但作为最后的手段,只需尝试简单:

  • defaultCredentials="false"
  • 的DefaultCredentials = “假”

#5


0  

What does User.Identity.IsAuthenticated return, if it returns false that is your problem. You are trying to send mail as an unauthenticated user.

User.Identity.IsAuthenticated返回什么,如果它返回false就是你的问题。您正在尝试以未经身份验证的用户身份发送邮件。

#6


0  

Had the same issue - the credentials from ASP.Net were never going to be something that could send e-mail in my environment. So I figured out this path through the mess (which also includes the possibility that NTLM doesn't always work right and that I was putting the mail configuration info in web.config):

有同样的问题 - 来自ASP.Net的凭证永远不会成为可以在我的环境中发送电子邮件的东西。所以我在混乱中找到了这条路径(其中还包括NTLM并不总是正常工作的可能性以及我将邮件配置信息放在web.config中):

System.Net.Configuration.SmtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;

// set up SMTP client
SmtpClient smtp = new SmtpClient();
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
NetworkCredential myCred = new NetworkCredential(section.Network.UserName, section.Network.Password);
string host = section.Network.Host;
int port = section.Network.Port;

// do this because NTLM doesn't work in all environments....
if (myCred != null)
{
    myCache.Add(host, port, "Digest", myCred);
    myCache.Add(host, port, "Basic", myCred);
    myCache.Add(host, port, "Digest-MD5", myCred);
    myCache.Add(host, port, "Digest MD5", myCred);
    myCache.Add(host, port, "Plain", myCred);
    myCache.Add(host, port, "Cram-MD5", myCred);
    myCache.Add(host, port, "Cram MD5", myCred);
    myCache.Add(host, port, "Login", myCred);

    //myCache.Add(host, port, "NTLM", myCred);
}
smtp.Credentials = myCache;
smtp.UseDefaultCredentials = false;
//smtp.EnableSsl = true;

Depending on your configuration, you might need to uncomment the last line.

根据您的配置,您可能需要取消注释最后一行。

#7


0  

For me it was using different credentials -

对我来说,它使用不同的凭据 -

In network credentials I was using -

在我使用的网络凭证中 -

new NetworkCredential(smtpUser, smtpPassword)

and in fromAddress

并在fromAddress中

 var fromAddress = new MailAddress("anotheruser@gmail.com", string.Empty);

Where when sending email where fromAddress is different than actual network credentials will lead this issue. See below -

在发送电子邮件时,fromAddress与实际网络凭据不同将导致此问题。见下文 -

 using (var message = new System.Net.Mail.MailMessage(fromAddress, toAddress)
                {
                    Subject = Keys.MailSubject,
                    Body = body,
                    IsBodyHtml = true
                })
                {
                    smtp.Send(message);

                }

The simple fix is to keep both the mails same as network credentials's one.

简单的解决方法是将两个邮件保持为网络凭证的一个。

#8


0  

One thing worked for me using Visual studio 2013 running packages. i removed the 'Use Windows Authentication' tag in the SMTP Connection Manager Editor.

使用Visual Studio 2013运行包有一件事对我有用。我删除了SMTP连接管理器编辑器中的“使用Windows身份验证”标记。