【问题标题】:MimeKit azure gmail -> Authentication failedMimeKit azure gmail -> 身份验证失败
【发布时间】:2017-05-13 06:19:20
【问题描述】:

我在 aspnet 核心上使用 MimeKit,一切都在本地运行,但是当我尝试在 azure App Service 上发送电子邮件时,我得到 “身份验证失败”

我必须启用“允许安全性较低的应用程序”才能让它在本地工作。

这是我正在使用的代码:

        using (var client = new SmtpClient())
        {
            await client.ConnectAsync("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
            await client.AuthenticateAsync(_emailOptions.Username, _emailOptions.Password);
            await client.SendAsync(emailMessage);
            await client.DisconnectAsync(true);
        }

我已通过将 _emailOptions.Username 和 _emailOptions.Password 记录在抛出的异常中来验证它们是正确的

        catch (Exception ex)
        {
            _log.LogError(new EventId(1), ex, "Username: '{0}', Password: '{1}'", _emailOptions.Username, _emailOptions.Password);
            throw;
        }

您对如何解决此问题有任何想法吗?

【问题讨论】:

  • 我想知道您是否在 azure 和本地应用程序上使用 same 用户名和密码。
  • 是的,我尝试硬编码用户名/密码。它可以在本地工作,但不能在 azure 中工作。

标签: azure asp.net-core azure-web-app-service mimekit


【解决方案1】:

我创建了一个示例来使用 MimeKit 和 MailKit 电子邮件框架发送电子邮件,如果我提供有效的用户名和密码并允许不太安全的应用程序访问 gmail 帐户(该帐户不使用 2-Step验证)。

using (var client = new SmtpClient())
{
    MimeMessage mes = new MimeMessage();
    mes.From.Add(new MailboxAddress("xxx", "xxx@gmail.com"));
    mes.To.Add(new MailboxAddress("xxx", "xxx@hotmail.com"));
    mes.Subject = "hello";
    mes.Body = new TextPart("plain")
    {
        Text = @"hi,
        i'm azure! " + DateTime.Now.ToString()
    };

    client.Connect("smtp.gmail.com", 465, SecureSocketOptions.SslOnConnect);
    client.Authenticate("username here", "password here");
    client.Send(mes);
    client.Disconnect(true);
}

请确保您提供的帐户是否使用两步验证或未启用安全性较低的应用访问帐户。另外SendGrid是一个云端邮件服务,你可以试试。

【讨论】:

  • 这很奇怪。除了我使用的是异步版本之外,我们的代码看起来都一样。
  • 在启用两因素身份验证并生成应用程序特定密码后,我让它工作。由于您的代码正在运行,因此我正在给您正确的答案。
【解决方案2】:

感谢指导。我对 azure app 和 gmail 有同样的问题。阅读此页面后,我打开了两步验证并生成了应用专用密码。这也解决了我的问题。

【讨论】:

    猜你喜欢
    • 2010-09-26
    • 2021-05-22
    • 1970-01-01
    • 2022-11-04
    • 2019-03-14
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多