我正在构建一个 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
有什么建议吗?