c# - 有没有办法在不选择 'Allow less secure apps' 选项的情况下使用 ASP.NET 通过 Google Apps 帐户发送电子邮件?

标签 c# asp.net google-apps

我有一个 ASP.NET Web API,我正试图用它来发送电子邮件。我在使用 smtp.gmail.com 和 587 时遇到身份验证问题。我看到几个链接说要在我的 Google Apps 帐户上切换“允许安全性较低的应用程序”选项,但我不想向所有者解释为什么他们需要选择这个听起来可疑的设置。

有没有办法在 ASP.NET 中打包 Google 喜欢的电子邮件请求,或者我是否必须选择“允许安全性较低的应用程序”选项?

这是我正在使用但 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);
}

最佳答案

谷歌在一段时间前(一两年前)改变了使用其 SMTP 服务器的可能性。 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.

就这些。

关于c# - 有没有办法在不选择 'Allow less secure apps' 选项的情况下使用 ASP.NET 通过 Google Apps 帐户发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699405/

相关文章:

c# - 插入数据库时​​如何以编程方式覆盖 Auto_Increment 值

python - 使用 Google Apps api 的模拟进行单元测试 - 管理目录 API

php - 以编程方式评估域是否具有 Google Apps

c# - 以编程方式在 Outlook 2007 中创建 IMAP 帐户

c# - 非事务性队列上的 MSMQ 错误

c# - 如果在 ASP.NET MVC 3 中有空间,为什么文件名 header 不完整?

asp.net - 无需重新发布即可从 CodeBehind 跳转到 Anchor

c# - 尝试了解 "Common"与 ASP.NET 和 HttpClient() 的异步死锁

c# - telerik radgrid GridView1_ItemInserted - 我可以获得返回值吗

google-apps - 将 Google 帐户登录限制到指定的 Apps 域