邮件发送失败。无法连接到远程服务器。套接字操作
Failure Sending Mail. Unable to connect to remote server. A socket operation
所以我试图通过 VB.net 代码发送电子邮件,但我一直收到此错误。我已经禁用了我的防火墙,当它没有从文本框中获取文本时,我可以成功发送电子邮件。我知道我有正确的设置,因为当我发送一封正文为 1 行的电子邮件时它会起作用。
Private Sub SendMessage(ByVal SmtpHost As String, ByVal SmtpPort As Integer, ByVal ssl As Boolean, ByVal SmtpUsername As String, ByVal SmtpPassword As String, ByVal mail_from As String, ByVal display_name As String, ByVal Send_To As String, ByVal subject As String, ByVal Body As String, Optional ByVal Attachments As String() = Nothing)
Using smtp As New SmtpClient
smtp.Host = SmtpHost
smtp.Port = SmtpPort
smtp.UseDefaultCredentials = False
smtp.Credentials = New Net.NetworkCredential(SmtpUsername, SmtpPassword)
smtp.EnableSsl = ssl
Dim message As New MailMessage()
Try
message.From = New MailAddress(mail_from, display_name, System.Text.Encoding.UTF8)
message.To.Add(Send_To)
message.Subject = subject
message.Body = Body
If Attachments IsNot Nothing Then
For Each attachment As String In Attachments
message.Attachments.Add(New Attachment(attachment))
Next
End If
message.ReplyToList.Add(New MailAddress(mail_from))
smtp.Send(message)
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Using
错误:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network 0.0.0.1:465
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailSender.Form1.Timer2_Tick(Object sender, EventArgs e)
您的代码可能没有任何错误。但是,如果您的邮件帐户配置不正确,则会出现此特定 'Failure Sending Mail'。您必须允许访问第三方或外部应用程序才能使用邮件的 SMTP 服务。您可以在邮件设置中更改它。
改变它并尝试。这可能有用。
Try
Dim mMailMessage As MailMessage = New System.Net.Mail.MailMessage()
Dim fromEmail As String = "<sender_mail_ID"
Dim fromPW As String = "<sender_password>"
Dim toEmail As String = "<Receiver_mail_ID>"
mMailMessage.From = New MailAddress(fromEmail)
mMailMessage.[To].Add(toEmail)
mMailMessage.Subject = "<Subject goes here>"
mMailMessage.Body = "<Message AKA Body goes here>"
mMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
Dim smtpClient As New SmtpClient("smtp.gmail.com", 587)
smtpClient.EnableSsl = True
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New NetworkCredential(fromEmail, fromPW)
smtpClient.Send(mMailMessage.From.ToString(), mMailMessage.[To].ToString(), mMailMessage.Subject, mMailMessage.Body)
Catch ex As Exception
End Try
别忘了使用:
Imports System.Net.Mail
Imports System.Net
我的 2 美分。在我的例子中,我把所有的东西都布置得很完美,但让我失望的是,我没有意识到我在后台有 privateinternetaccess 应用程序 运行,它阻止了每一次连接到服务器的尝试。不酷:(
所以我试图通过 VB.net 代码发送电子邮件,但我一直收到此错误。我已经禁用了我的防火墙,当它没有从文本框中获取文本时,我可以成功发送电子邮件。我知道我有正确的设置,因为当我发送一封正文为 1 行的电子邮件时它会起作用。
Private Sub SendMessage(ByVal SmtpHost As String, ByVal SmtpPort As Integer, ByVal ssl As Boolean, ByVal SmtpUsername As String, ByVal SmtpPassword As String, ByVal mail_from As String, ByVal display_name As String, ByVal Send_To As String, ByVal subject As String, ByVal Body As String, Optional ByVal Attachments As String() = Nothing)
Using smtp As New SmtpClient
smtp.Host = SmtpHost
smtp.Port = SmtpPort
smtp.UseDefaultCredentials = False
smtp.Credentials = New Net.NetworkCredential(SmtpUsername, SmtpPassword)
smtp.EnableSsl = ssl
Dim message As New MailMessage()
Try
message.From = New MailAddress(mail_from, display_name, System.Text.Encoding.UTF8)
message.To.Add(Send_To)
message.Subject = subject
message.Body = Body
If Attachments IsNot Nothing Then
For Each attachment As String In Attachments
message.Attachments.Add(New Attachment(attachment))
Next
End If
message.ReplyToList.Add(New MailAddress(mail_from))
smtp.Send(message)
Catch ex As Exception
Debug.WriteLine(ex)
End Try
End Using
错误:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network 0.0.0.1:465
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
--- End of inner exception stack trace ---
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at EmailSender.Form1.Timer2_Tick(Object sender, EventArgs e)
您的代码可能没有任何错误。但是,如果您的邮件帐户配置不正确,则会出现此特定 'Failure Sending Mail'。您必须允许访问第三方或外部应用程序才能使用邮件的 SMTP 服务。您可以在邮件设置中更改它。
改变它并尝试。这可能有用。
Try
Dim mMailMessage As MailMessage = New System.Net.Mail.MailMessage()
Dim fromEmail As String = "<sender_mail_ID"
Dim fromPW As String = "<sender_password>"
Dim toEmail As String = "<Receiver_mail_ID>"
mMailMessage.From = New MailAddress(fromEmail)
mMailMessage.[To].Add(toEmail)
mMailMessage.Subject = "<Subject goes here>"
mMailMessage.Body = "<Message AKA Body goes here>"
mMailMessage.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
Dim smtpClient As New SmtpClient("smtp.gmail.com", 587)
smtpClient.EnableSsl = True
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
smtpClient.UseDefaultCredentials = False
smtpClient.Credentials = New NetworkCredential(fromEmail, fromPW)
smtpClient.Send(mMailMessage.From.ToString(), mMailMessage.[To].ToString(), mMailMessage.Subject, mMailMessage.Body)
Catch ex As Exception
End Try
别忘了使用:
Imports System.Net.Mail
Imports System.Net
我的 2 美分。在我的例子中,我把所有的东西都布置得很完美,但让我失望的是,我没有意识到我在后台有 privateinternetaccess 应用程序 运行,它阻止了每一次连接到服务器的尝试。不酷:(