簡體   English   中英

如何在 Asp.Net Core 中使用 MailKit 發送 email

[英]how to send email using MailKit in Asp.Net Core

我不確定使用 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);
            }

但是如果我使用上面的代碼發送 email

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 修復上面的代碼

像這樣試試。

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM