【问题标题】:how to send email using MailKit in Asp.Net Core如何在 Asp.Net Core 中使用 MailKit 发送电子邮件
【发布时间】:2020-12-04 10:32:01
【问题描述】:

不确定使用 Mailkit 发送邮件的旧方法是否适用于下面的代码

try
            {
                var emailMessage = new MimeMessage();
                emailMessage.From.Add(new MailboxAddress(_emailConfig.SenderName, _emailConfig.SenderAddress));
                emailMessage.To.Add(new MailboxAddress(email));
                emailMessage.Subject = subject;
                var builder = new BodyBuilder
                {
                    HtmlBody = message
                };
                emailMessage.Body = builder.ToMessageBody();
                using var smtp = new SmtpClient
                {
                    ServerCertificateValidationCallback = (s, c, h, e) => true
                };
                smtp.AuthenticationMechanisms.Remove("XOAUTH2");

                await smtp.ConnectAsync(_emailConfig.SmtpServer, Convert.ToInt32(_emailConfig.Port), false).ConfigureAwait(false);
                await smtp.AuthenticateAsync(_emailConfig.Username, _emailConfig.Password).ConfigureAwait(false);
                await smtp.SendAsync(emailMessage).ConfigureAwait(false);
                await smtp.DisconnectAsync(true).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(ex.Message);
            }

但如果我使用上面的代码发送电子邮件,则会出现异常

nvalidOperationException: 534: 5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv
5.7.14 26mzQKtlwfyEdGzHHdpi3ewWG6skAWgOBbdNNYmwzr9Sg3fGu-KixLAfODpJsVafutidE
5.7.14 8xBOp_8rNCvk9Y6iEcOkDlcZ1d-483zQ1Krw04NvqxQdq3w4iTtC8E9bL8uGprgV>
5.7.14 Please log in via your web browser and then try again.
5.7.14 Learn more at
5.7.14 https://support.google.com/mail/answer/78754 o5sm2555896wmh.8 - gsmtp

所以当我将下面这行代码更改为使用 SSLS 时,出现了一个新错误

await smtp.ConnectAsync(_emailConfig.SmtpServer, Convert.ToInt32(_emailConfig.Port), true).ConfigureAwait(false);

异常返回

InvalidOperationException: An error occurred while attempting to establish an SSL or TLS connection.

This usually means that the SSL certificate presented by the server is not trusted by the system for one or more of
the following reasons:

1. The server is using a self-signed certificate which cannot be verified.
2. The local system is missing a Root or Intermediate certificate needed to verify the server's certificate.
3. A Certificate Authority CRL server for one or more of the certificates in the chain is temporarily unavailable.
4. The certificate presented by the server is expired or invalid.

Another possibility is that you are trying to connect to a port which does not support SSL/TLS.

It is also possible that the set of SSL/TLS protocols supported by the client and server do not match.

See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#SslHandshakeException for possible solutions.

到处搜索如何做到这一点,甚至打开了我不太安全的应用程序。一些推荐的 sendGrid,我也用他们创建了一个免费帐户,但我无权访问创建的帐户。有谁知道如何使用 Mailkit 修复上面的代码

【问题讨论】:

  • 您是否访问过错误消息中提到的support.google.com/mail/answer/78754,以及它所暗示的任何事情?您的(第一个)问题似乎是您没有正确配置 Gmail 帐户以接受来自应用程序的连接(而不是来自传统用户登录)。
  • 另外,如果你只是用谷歌搜索“mailkit gmail”,你会得到很多可能相关的建议。

标签: c# asp.net-core ssl


【解决方案1】:

这样试试吧。

using (var smtpClient = new SmtpClient())
{
   smtpClient.ServerCertificateValidationCallback = (s, c, h, e) => true;
   await smtpClient.ConnectAsync("host", port, false);

   if (smtpClient.Capabilities.HasFlag(SmtpCapabilities.Authentication))
   {
      smtpClient.AuthenticationMechanisms.Remove("XOAUTH2");
      await smtpClient.AuthenticateAsync(username, password);
   }

   await smtpClient.SendAsync(mailMsg);
   smtpClient.Disconnect(true);
}

【讨论】:

    猜你喜欢
    • 2019-02-12
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2019-11-23
    • 2019-10-14
    • 2021-01-30
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多