有没有一种方法可以在不选择 'Allow less secure apps' 选项的情况下使用 ASP.NET 通过 Google Apps 帐户发送电子邮件?

Is there a way to use ASP.NET to send email through a Google Apps account without selecting the 'Allow less secure apps' option?

我有一个 ASP.NET 网络 API,我想用它来发送电子邮件。我在使用 smtp.gmail.com 和 587 时遇到身份验证问题。我看到几个链接说要在我的 Google Apps 帐户上切换 'Allow less secure apps' 选项,但我不想解释对所有者为什么他们需要选择这个听起来可疑的设置。

有没有办法在 ASP.NET 中打包 Google 会喜欢的电子邮件请求,或者我必须 select 'Allow less secure apps' 选项?

这是我正在使用的代码,Google 不喜欢。

using (var client = new SmtpClient("smtp.googlemail.com", 587))
{
    client.Credentials = 
        new System.Net.NetworkCredential("address@yourdomain.com", "yourpassword");
    client.EnableSsl = true;
    var msg = new MailMessage("address@yourdomain.com", "toaddress@anotherdomain.com");
    msg.Body = "[YOUR EMAIL BODY HERE]";
    msg.Subject = "[Message Subject]";

    client.Send(msg);
}

Google 前段时间(一两年前)更改了使用其 SMTP 服务器的可能性。 Google 现在需要更高级别的安全和身份验证。 然而,仍然可以在没有 'Allow less secure apps' 选项的情况下使用它,但是你不能使用标准的 google 用户名和密码。

操作方法(简而言之)如下:

  1. Turn on 2-Step Verification in your gmail or google apps account in “My account / Sign-in & security”,
  2. then (in “My account / Sign-in & security / App passwords”) it is possible to generate a special password for access from other applications (without turned on 2-Step Verification you can not get there)
  3. this created app password (and your google e-mail account as a username) you can then use as your SMTP login.

就这些了。