使用SmtpClient发送电子邮件时出现“邮箱不可用。服务器响应为: 拒绝访问 - 无效的HELO名称”故障排除

13

我一直在尝试通过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:邮箱不可用。服务器响应为:访问被拒绝 - 无效的HELO名称(请参阅RFC2821 4.1.1.1)

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


看起来你正在尝试连接的服务器需要你的客户端进行身份验证... - lc.
2
这是一个打字错误吗?string to = receiver@domain.com; - Chase Florell
6个回答

10

看起来您的用户名/密码对未能成功通过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);

在这种情况下,他的代码确实有问题。如果他的代码没有提供某些信息,那么代码是“错误的”。 - Gertjan
@Gertjan:是的,我同意你的观点。已更新答案。 - this. __curious_geek
谢谢!问题已解决!没想到是SMTP端设置的一些奇怪设置。 - user303907
@user303907:虽然这条消息可能看起来很奇怪,但客户端通过向服务器发送HELO命令进行身份验证,因此如果服务器无法登录您,它会给您一个访问被拒绝的错误(就像现在发生的情况),并且有些服务器可能会告诉您出现问题的位置(在对话的HELO部分)。 - Gertjan
1
我遇到了同样的问题。之前它是正常工作的,但现在客户抱怨这个问题。客户说他们没有更改任何设置。有什么建议吗? - Jagz W

4
你尝试过在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>

还有你的后端代码

MailMessage message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");
message.To.Add(new MailAddress("recipient1@foo.bar.com"));
message.To.Add(new MailAddress("recipient2@foo.bar.com"));
message.To.Add(new MailAddress("recipient3@foo.bar.com"));
message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";
SmtpClient client = new SmtpClient();
client.Send(message);

4

试试这个:

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

谢谢Paul,UseDefaultCredentials = false; 对我有用。 - undefined

2
在我的情况下,是端口设置错误。主机提供的配置无论是SSL(465)还是非SSL(25)都无法工作。 我使用MS Outlook来“破解”配置,然后将其复制到我的应用程序中。正确的端口是587 SSL。

1
这可能发生在您没有设置EnableSsl的情况下。
client.EnableSsl = true;

1
这解决了我的问题。文档中写的是非 SSL 连接端口 587,但是会返回 HELO 错误。将 SSL 切换为 true 后就可以了 :) - luka_matkic

0

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

enter image description here

我们遇到了异常情况,请求我们的托管公司进入以下位置:
“Root WHM > 服务配置 > Exim 配置管理器 > 基本编辑器 > ACL 选项”
并将“要求符合 RFC 的 HELO”设置为“关闭”。
在修复下一个错误后,这对我们起作用了:
“在端口 587 上需要 SMTP AUTH 来提交消息。”
来源:

https://serverfault.com/a/912351/293367


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接