【问题标题】:The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.?8SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:5.5.1 Authentication Required.?8
【发布时间】:2015-05-18 10:16:09
【问题描述】:

我正在使用 c# 创建示例电子邮件发件人,但它显示错误是:

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.5.1 Authentication Required。

        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
        mail.To.Add("toAddress@gmail.com");
        mail.From = new MailAddress("fromAddress@gmail.com", "Test Mail !", System.Text.Encoding.UTF8);
        mail.Subject = "Test Mail";
        mail.SubjectEncoding = System.Text.Encoding.UTF8;
        mail.Body = "Test message";
        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.IsBodyHtml = true;
        mail.Priority = MailPriority.High;
        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("myMail@gmail.com", "password");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.UseDefaultCredentials = true;
        client.Send(mail);

网络配置:

<system.net>
    <mailSettings>
      <smtp from="myMail@gmail.com">
        <network host="smtp.gmail.com" password="password"
          port="587" userName="myMail@gmail.com" />
      </smtp>
    </mailSettings>
</system.net>

【问题讨论】:

标签: c# email authentication


【解决方案1】:

如果您启用了双重身份验证,则需要为自己的 Google 帐户生成应用密码。

生成该密码的步骤如下([Google page] | [Archived page]):

  1. 访问您的App passwords 页面。系统可能会要求您登录自己的 Google 帐户。
  2. 在底部,点击选择应用并选择您正在使用的应用。
  3. 点击选择设备并选择您正在使用的设备。

  1. 点击生成。
  2. 按照说明在您的设备上输入应用密码(黄色栏中的 16 个字符的代码)。

  1. 点击完成。

点击完成后,您将不会再看到该应用密码代码。 但是,您将看到您创建的应用程序和设备的列表 的密码。

然后,在您的 NetworkCredentials 中,将您的密码替换为生成的 16 位密码。

【讨论】:

    【解决方案2】:

    尝试通过代码发送邮件,当您收到错误消息时,登录您的 gmail 帐户并在 2 或 3 分钟后检查收件箱。你会收到这样的邮件:

    #;
    #;
    

    #;
    #;
    

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2015-05-01
      • 2017-02-17
      • 2013-10-31
      • 2014-01-21
      • 2014-11-08
      • 2016-05-16
      相关资源
      最近更新 更多