2016-12-26 11 views
1
public static string Send(string to, string subject, string content, string from = "") 
     { 
      try 
      { 
       MimeMessage message = new MimeMessage(); 
       message.Subject = subject; 
       message.Body = new TextPart("Plain") { Text = content }; 
       message.From.Add(new MailboxAddress(from)); 
       message.To.Add(new MailboxAddress(to)); 

       SmtpClient smtp = new SmtpClient(); 
       smtp.Connect(
         "smtp.live.com" 
        , 587 
        , MailKit.Security.SecureSocketOptions.StartTls 
       ); 
       smtp.Authenticate("[email protected]", "Password"); 
       smtp.Send(message); 
       smtp.Disconnect(true); 
       return "Success"; 
      } 
      catch (Exception ex) 
      { 
       return $"Failed. Error: {ex.Message}"; 
      } 
     } 
gmailの

ASP .NETのコア電子メール

smtp.Connect(
    "smtp.gmail.com" 
    , 587 
    , MailKit.Security.SecureSocketOptions.StartTls 
); 

を使用して


を送信するためにMailKitを使用して、私は他のウェブサイトの記事からいくつかのプロパティを変更しようとしました。

しかし、私は通常、これらのエラーメッセージを取得:

  1. は、 "失敗しましたエラー:SMTPサーバが認証をサポートしていません。"

  2. "失敗:エラー: 検証手順に従ってリモート証明書が無効です。"

プロパティを正しく設定するにはどうすればよいですか?

答えて

4

ASP CoreでMimeKitを使用してGmailに送信します。ここに私のプロジェクトのスニペットがあります:

using (var client = new SmtpClient()) 
{ 
client.Connect(_appSettings.SmtpServerAddress, _appSettings.SmtpServerPort, SecureSocketOptions.StartTlsWhenAvailable); 
client.AuthenticationMechanisms.Remove("XOAUTH2"); // Must be removed for Gmail SMTP 
client.Authenticate(_appSettings.SmtpServerUser, _appSettings.SmtpServerPass); 
client.Send(Email); 
client.Disconnect(true); 
} 
+0

答えていただきありがとうございます。 – Gary

関連する問題