【问题标题】:VB.NET/ASP.NET Send Automated Gmail Outgoing Messages with OAuthVB.NET/ASP.NET 使用 OAuth 发送自动 Gmail 外发邮件
【发布时间】:2019-12-23 04:29:46
【问题描述】:

我的 VB.NET/ASP.NET 应用程序使用以下代码向我的用户发送自动电子邮件。我收到来自 Google 的电子邮件,他们将停止“不太安全”的应用程序并要求 OAuth。所有电子邮件都会自动发送,用于提醒即将到来的约会提醒和约会更改。我无法在每次应用程序需要发送电子邮件时都需要手动登录电子邮件帐户。

这是我当前的代码。谁能指导我如何修改它以使用在 Google Developer Console 中为我的 Gmail 电子邮件帐户生成的凭据?

Public Shared Sub SendEmailInBackground(收件人为字符串,主题为字符串,消息为字符串)

    Dim server As String = ConfigurationManager.AppSettings("SmtpHostname")
    Dim username As String = ConfigurationManager.AppSettings("SmtpUsername")
    Dim password As String = ConfigurationManager.AppSettings("SmtpPassword")

    Dim email As New System.Net.Mail.MailMessage(username, Recipient)
    email.Subject = Subject
    email.Body = Message

    Dim client As New System.Net.Mail.SmtpClient(server, 587)
    client.Credentials = New System.Net.NetworkCredential(username, password)

    client.EnableSsl = True
    client.Send(email)

结束子

【问题讨论】:

  • Using Gmail SMTP OAUTH。我建议你关闭那里的old SmtpClient class(见横幅)并选择一个新的。有关建议,请参阅备注部分。 MimeKit也可以。
  • 感谢您的回复。我一直与 EASMail 支持人员联系,他们解释说代码需要交互性 - 每次应用程序想要发送电子邮件时,使用电子邮件/密码交互登录以接收令牌。我的应用程序一直在发送自动电子邮件,以提醒人们即将到来的约会和约会更改,并且需要通过一种安全的方式进行补救,以使用像服务帐户一样运行的合规性 GMail。关于如何进行的任何建议?

标签: asp.net vb.net gmail google-oauth gmail-api


【解决方案1】:

通过快速入门步骤,您可以设置 oauth [1],然后使用您从那里获得的经过身份验证的服务发出发送电子邮件 [2] 请求。快速入门中使用经过身份验证的 GmailService 的文档示例:

using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;

// ...

public class MyClass {

  // ...

  /// <summary>
  /// Send an email from the user's mailbox to its recipient.
  /// </summary>
  /// <param name="service">Gmail API service instance.</param>
  /// <param name="userId">User's email address. The special value "me"
  /// can be used to indicate the authenticated user.</param>
  /// <param name="email">Email to be sent.</param>
  public static Message SendMessage(GmailService service, String userId, Message email)
  {
      try
      {
          return service.Users.Messages.Send(email, userId).Execute();
      }
      catch (Exception e)
      {
          Console.WriteLine("An error occurred: " + e.Message);
      }

      return null;
  }

  // ...

}

[1]https://developers.google.com/gmail/api/quickstart/dotnet

[2]https://developers.google.com/gmail/api/v1/reference/users/messages/send

【讨论】:

    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多