12

我一直在尝试通过 C# 发送电子邮件。我已经在 Google 上搜索了各种示例,并从每个示例以及每个人最有可能使用的标准代码中获取了点点滴滴。

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 服务器上工作吗?

4

6 回答 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);
于 2010-07-01T05:21:32.497 回答
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);
于 2010-07-01T05:22:07.697 回答
3

试试这个:

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);
于 2010-07-01T05:26:18.563 回答
2

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

于 2017-08-14T23:19:06.987 回答
1

如果您未设置 EnableSsl,则可能会发生这种情况。

client.EnableSsl = true;
于 2020-03-18T17:30:41.703 回答
0

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

在此处输入图像描述

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

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

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

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

在端口 587 上提交邮件需要 SMTP AUTH

来源:

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

于 2020-09-01T15:55:25.273 回答