2017-04-23 102 views
3

我有一個關於在asp.net核心上發送電子郵件的問題。Asp.Net Core MailKit:根據驗證過程,遠程證書無效

所以我有這樣的代碼:

private void SendEmailLocalSMTP(string email, string subject, string message) 
{ 
     MimeMessage mailMsg = new MimeMessage(); 

     //TESTING ENVIRONMENT ONLY!!! 
     mailMsg.To.Add(new MailboxAddress("[email protected]", "[email protected]")); 

     // From 
     mailMsg.From.Add(new MailboxAddress("[email protected]", "Facturacion")); 

     // Subject and multipart/alternative Body 
     mailMsg.Subject = subject; 
     string html = message; 

     System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("[email protected]", "myPassword"); 

     // Init SmtpClient and send 
     using (var client = new SmtpClient()) 
     { 
      client.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls); 
      client.AuthenticationMechanisms.Remove("XOAUTH2"); 
      client.Authenticate(credentials); 
      client.Send(mailMsg); 
      client.Disconnect(true); 
     } 
    } 

從我在其他職位有點關係發現,這應該是足夠使用MailKit發送它,但它不能正常工作。我得到以下例外,我不知道如何從這裏開始。

這是例外:

enter image description here

我已經看到了這個問題,但我還沒有從中取得多大意義:How to send email by using MailKit?

任何幫助,高度讚賞,感謝。

回答

3

您遇到的問題是您的系統不信任GMail SSL證書。

最有可能的原因是因爲Google根CA證書尚未導入您的根證書存儲區。

這種情況有解決方法有兩種:

  1. 導入谷歌的根CA證書到系統(如何做到這一點取決於在Windows,Linux,MacOS的變化)
  2. 覆蓋證書驗證通過設定自己在client.ServerCertificateValidationCallback

欲瞭解更多信息,證書驗證回調方法,請參見常見問題解答:https://github.com/jstedfast/MailKit/blob/master/FAQ.md#InvalidSslCertificate

+0

謝謝!很好的答案! –

相關問題