邮件发送失败。无法连接到远程服务器。套接字操作
Posted
技术标签:
【中文标题】邮件发送失败。无法连接到远程服务器。套接字操作【英文标题】:Failure Sending Mail. Unable to connect to remote server. A socket operation 【发布时间】:2015-06-09 02:11:50 【问题描述】:所以我试图通过 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)
【问题讨论】:
你没有提到你得到的错误......另外请澄清这一点,“但是当我在文本框中输入时,它不仅仅是一行,可能是这样吗?我怎样才能做到这一点'将发送多行电子邮件?这没有意义...... 我更改了 Smtp 电子邮件代码,但仍然遇到同样的问题,我用错误更新了问题。 你有计时器吗? ... 是的,我有一个计时器在做这个,看看它是否能正常工作......但是它甚至没有一次与计时器一起工作。 【参考方案1】:您的代码可能没有任何错误。但是,如果您的邮件帐户配置不正确,则会发生这种特定的“发送邮件失败”。您必须允许访问第三方或外部应用程序才能使用您邮件的 SMTP 服务。您可以在邮件设置中更改它。
改变它并尝试。这可能行得通。
【讨论】:
我认为这也是原因,因为我记得过去必须这样做。我会四处寻找,然后回复发生的情况。 是否允许访问不太安全的应用程序?如果是这样,我已经打开了。 没有。你不必那样做。只允许 SMTP 访问。您使用哪种服务发送邮件?我的意思是像 gmail 或任何其他? 我正在使用 Gmail,是的,先生 好吧,我发现端口 25 和 465 不起作用,因为它无法通过 telnet。所以代码运行良好,它只是我的电脑。【参考方案2】: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
【讨论】:
【参考方案3】:我的 2 美分。在我的情况下,我把所有东西都布置得很完美,什么让我没有意识到我在后台运行了 privateinternetaccess 应用程序,这阻止了每次连接到服务器的尝试。不酷:(
【讨论】:
以上是关于邮件发送失败。无法连接到远程服务器。套接字操作的主要内容,如果未能解决你的问题,请参考以下文章
紧急求助,在传输数据时页面提示无法连接到远程服务器 System.Net.Sockets.SocketException
Java - 异步套接字通道无法连接到远程异步服务器套接字通道