リモート/分離されたマシンで実行され、管理者に電子メールでエラー通知を送信することになっている Windows フォーム アプリケーションを構築しています。これを達成するためにクラスを採用しようとしましSystem.Net.Mail
たが、奇妙な問題に直面しています:
1.エラー メッセージが表示されます。
System.IO.IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.--->
System.Net.Sockets.SocketException: An existing connection was forcibly closed by
the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.
Read(Byte[] buffer, Int32 offset, Int32 size)
2.ネットワーク アクティビティをスニッフィングして、何が問題なのかを調べてみました。それで、それがどのようになるかは次のとおりです。
i) The DNS lookup for my SMTP server's hostname works
ii) My application connects to the SMTP server and sends "EHLO MY-HOSTNAME"
iii) SMTP server responds back with it's usual
iv) My application sends "AUTH login abcdxyz" and receives an acknowledgement packet
この時点で、SMTP サーバーがパスワードを要求していないように見えるか、SMTP サーバーがパスワードを要求する前にマシンが SMTP サーバーへの接続を遮断したようです。
別の SMTP ポートと SMTP ホストを使用してみました。また、ファイアウォールと AV を無効にしようとしましたが、うまくいきませんでした。PuTTY を使用して SMTP サーバーに接続し、アプリケーションと同じコマンド シーケンス (パケット スニファーから選択) を発行すると、すべてが正常に機能し、電子メールを送信できます。
私が使用しているコードは次のとおりです。
Imports System.Net
Imports System.Net.Mail
Public Function SendMail() As Boolean
Dim smtpClient As New SmtpClient("smtp.myserver.com", 587) 'I tried using different hosts and ports
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New NetworkCredential("username@domain.com", "password")
smtpClient.EnableSsl = True 'Also tried setting this to false
Dim mm As New MailMessage
mm.From = New MailAddress("username@domain.com")
mm.Subject = "Test Mail"
mm.IsBodyHtml = True
mm.Body = "<h1>This is a test email</h1>"
mm.To.Add("someone@domain.com")
Try
smtpClient.Send(mm)
MsgBox("SUCCESS!")
Catch ex As Exception
MsgBox(ex.InnerException.ToString)
End Try
mm.Dispose()
smtpClient.Dispose()
Return True
End Function
何かアドバイス?