2017-03-20 7 views
1

私はAsp.netコアWebアプリケーションを持っているとMailkitを使用して電子メールを送信する:Asp.netコアmailkit

  emailMessage.From.Add(new MailboxAddress("John", "[email protected]")); 
      emailMessage.To.Add(new MailboxAddress("Doe", email)); 
      emailMessage.Subject = subject; 
      emailMessage.Body = new TextPart("plain") { Text = message }; 

      using (var client = new SmtpClient()) 
      { 
       client.ServerCertificateValidationCallback = (s, c, h, e) => true; 
       client.Connect("smtp-mail.outlook.com", 587, false); 
       client.AuthenticationMechanisms.Remove("XOAUTH2"); 
       client.Authenticate("[email protected]", "password"); 
       client.Send(emailMessage); 
       client.Disconnect(true); 
      } 

それがうまく送信だVS上とlocalYアプリケーションを実行中。しかし、私はそれがこのようなメッセージで失敗の電子メール送信時にAzureのアプリケーションサービスにアップロードする場合:

SmtpProtocolException: The SMTP server has unexpectedly disconnected. 

MailKit.Net.Smtp.SmtpStream.ReadAhead(CancellationToken cancellationToken) 

MailKit.Net.Smtp.SmtpStream.ReadResponse(CancellationToken cancellationToken) 
MailKit.Net.Smtp.SmtpClient.SendCommand(string command, CancellationToken cancellationToken) 
MailKit.Net.Smtp.SmtpClient.Authenticate(Encoding encoding, ICredentials credentials, CancellationToken cancellationToken) 
MailKit.MailService.Authenticate(string userName, string password, CancellationToken cancellationToken) 

誰もがそれを修正する方法を知っていますか?

答えて

0

Microsoft Azureは、ポート25での送信接続を許可しません。また、Microsoft Azureの単一の「祝福された」送信メールプロバイダであるSendGrid(サードパーティのSMTPプロバイダ)に接続する場合を除き、すべての送信SMTPをブロックします。

幸い(

SendGridは無料です(私はあなたが、Microsoftが表示さオフするアズールでのExchangeサービスを望むだけでなく、AWSの電子メールサービスと競合思うだろうので、これは奇妙だと思います) - そうでなければそれは強要だろう)月に2万メールまで、それから彼らはまだかなり安いです。

Azure Portal内から直接SendGridアカウントを作成し、[管理]ボタンをクリックしてSendGridアカウントの詳細を開き、ユーザー名とパスワードを含むSMTPの詳細を取得できます。

SendGridは2つのAPIを提供しています.1つ目は未処理の「ダム」SMTPサービスですが、デフォルトではアカウントが「信頼できる」とみなされるまでメールを10分間遅延させます。 2番目のAPIはHTTP Webサービスです。これは、HTTP接続がSMTP接続より安価であるため、負荷の高いシナリオではより効果的です。