【问题标题】:Cannot connect to smtp server that requires authentication无法连接到需要身份验证的 smtp 服务器
【发布时间】:2019-11-04 06:14:16
【问题描述】:

我正在尝试连接到需要身份验证的 smtp 服务器。

这是我用来发送电子邮件的 C# 代码:

var smtpClient = new SmtpClient();
smtpClient.Host = _smtpServerHost;
smtpClient.Credentials = new NetworkCredential(_username, _password);
smtpClient.UseDefaultCredentials = false;
smtpClient.Send(GetMessage());

发送函数引发错误:

The SMTP server requires a secure connection or the client was not authenticated. 
The server response was: Must authenticate before sending mail

我是否缺少连接到 smtp 服务器的步骤?

我尝试连接到我们正在运行的测试服务器,但没有成功。我还尝试连接到smtp4dev。当我不需要身份验证时,我可以连接到它,但在我需要身份验证时不能。

【问题讨论】:

  • 您确定您使用的凭据正确吗?
  • 我正在使用身份验证,但我没有使用 smtpClient.UseDefaultCredentials = false; 行 - 你试过删除它吗?
  • 你可以尝试直接telnet到25端口连接服务器,并尝试验证computerperformance.co.uk/exchange2003/…。或者,查看引发的异常,特别是内部异常

标签: c# .net smtp


【解决方案1】:

原来你需要设置UseDefaultCredentials = false你分配client.Credentials。如果你设置后它不会发送AUTH命令。

【讨论】:

    【解决方案2】:

    Microsoft 声明不推荐使用 SmtpClient。您应该使用 MailKit。 https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netframework-4.0

    我们不建议您将 SmtpClient 类用于新开发,因为 SmtpClient 不支持许多现代协议。请改用 MailKit 或其他库。有关详细信息,请参阅 SmtpClient 不应在 GitHub 上使用。

    【讨论】:

    猜你喜欢
    • 2015-08-27
    • 2013-09-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多