C# “故障排除”;邮箱不可用。服务器响应为:访问被拒绝-直升机名称无效”;使用SmtpClient发送电子邮件时

C# “故障排除”;邮箱不可用。服务器响应为:访问被拒绝-直升机名称无效”;使用SmtpClient发送电子邮件时,c#,smtpclient,C#,Smtpclient,我一直在试图通过C#发送电子邮件。我在谷歌上搜索了各种示例,并从每个示例以及每个人最可能使用的标准代码中提取了一些片段 string to = "receiver@domain.com"; string from = "sender@domain.com"; string subject = "Hello World!"; string body = "Hello Body!"; MailMessage message = new MailMessage(from, to, subject,

我一直在试图通过C#发送电子邮件。我在谷歌上搜索了各种示例,并从每个示例以及每个人最可能使用的标准代码中提取了一些片段

string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);
然而,我不断收到一个错误声明

System.Net.Mail.SmtpException:邮箱 不可用的服务器响应为: 拒绝访问-直升机名称无效(请参阅 RFC2821 4.1.1.1)


那么,我现在该怎么办?SmtpClient是否应该是特殊的,并且只能在特定的SMTP服务器上工作?

似乎您的用户名/密码对未在SMTP服务器上成功验证

编辑 我想,我发现这里出了什么问题。我已经在下面更正了你的版本

string to = "receiver@domain.com";

//It seems, your mail server demands to use the same email-id in SENDER as with which you're authenticating. 
//string from = "sender@domain.com";
string from = "test@domain.com";

string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("test@domain.com", "password");
client.Send(message);

您是否尝试过在web.Config中设置身份验证凭据

  <system.net>
    <mailSettings>
      <smtp from="test@foo.com">
        <network host="smtpserver1" port="25" userName="username" password="secret" defaultCredentials="true" />
      </smtp>
    </mailSettings>
  </system.net>
试试这个:

string to = "receiver@domain.com";
string from = "sender@domain.com";
string subject = "Hello World!";
string body =  "Hello Body!";
MailMessage message = new MailMessage(from, to, subject, body);
SmtpClient client = new SmtpClient("smtp.domain.com");
// explicitly declare that you will be providing the credentials:
client.UseDefaultCredentials = false;
// drop the @domain stuff from your user name: (The API already knows the domain
// from the construction of the SmtpClient instance
client.Credentials = new NetworkCredential("test", "password");
client.Send(message);

就我而言,这是一个错误的端口。主机提供的配置既不能使用SSL(465),也不能使用SSL(25)。
我使用MS Outlook“破解”配置,然后复制到我的应用程序中。它是587 SSL。

如果不设置EnableSsl,可能会发生这种情况

client.EnableSsl = true;

如果您有一个可用的webmail客户端,并且看到一个cPanel徽标,那么它也可能是一个设置

我们得到了一个例外,并要求我们的托管公司进入:

“根WHM>服务配置> Exim配置管理器>基本编辑器>ACL选项”

并将
Require RFC compliant HELO
设置设置为
Off

在修复下一个错误后,这对我们有效:

在端口587上提交邮件需要SMTP身份验证

资料来源:


看起来,无论您试图连接到哪个服务器,都需要您的客户端识别自身……这是一个打字错误吗<代码>字符串到=receiver@domain.com;client.EnableSsl = true;