邮件发送失败。无法连接到远程服务器。套接字操作 [英] Failure Sending Mail. Unable to connect to remote server. A socket operation

查看:524
本文介绍了邮件发送失败。无法连接到远程服务器。套接字操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图通过VB.net代码发送一封电子邮件,我一直收到这个错误。我禁用了防火墙,并且在没有从文本框中获取文本时,我可以成功发送电子邮件。我知道我有正确的设置,因为它发挥作用,当我发送电子邮件与1行的主体。

 私人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作为字符串,可选的ByVal附件作为字符串()=无)
使用smtp作为新的SmtpClient
smtp.Host = SmtpHost
smtp.Port = SmtpPort
smtp.UseDefaultCredentials = False
smtp.Credentials =新Net.NetworkCredential(SmtpUsername,SmtpPassword)
smtp.EnableSsl = ssl

Dim message As New MailMessage()
尝试
消息.From =新MailAddress(mail_from,display_name,System.Text.Encoding.UTF8)
message.To.Add(Send_To)
message.Subject = subject
message.Body = Body
如果Attachments IsNot Nothing Then
For Each attachment As String In Attachments
message.Attachments.Add(New Attachment(attachment))
下一个
结束如果
message.ReplyToList.Add(新MailAddress(mail_from))

smtp.Send(消息)
赶上例外
Debug.WriteLine(ex)
结束尝试
结束使用

错误:

  System.Net.Mail.SmtpException:发送邮件失败。 ---> System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:在System上的System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)
处尝试对无法访问的网络0.0.0.1:465
执行套接字操作。 Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState state,IAsyncResult asyncResult,Exception& exception)
---内部异常堆栈跟踪结束--- $在System.Net.Service.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress& address,Socket& abortSocket,Socket& abortSocket6)上的
在System.Net.PooledStream.Activate(Object owningObject,Boolean asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
在System.Net.Mail.SmtpClient.Send(MailMessage消息)
---内部异常堆栈跟踪结束---在System.Net.Mail.SmtpClient.Send(MailMessage消息)

at EmailSender.Form1.Timer2_Tick(Object sender,EventArgs e)


解决方案

 尝试
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 =新MailAddress(fromEmail)
mMailMessage。[To] .Add(toEmail)
mMailMessage.Subject =< Subject to here>
mMailMessage.Body =<消息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)

赶上例外

结束尝试



不要忘记使用:

  Imports System .Net.Mail 
Imports System.Net


So I'm trying to send an e-mail through VB.net code and I keep getting this error. I've disabled my firewall, and I can successfully send an e-mail when it's not getting the text from the textbox. I know that I have the right settings as it works when I send an e-mail with the body of 1 line.

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

Error:

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)

解决方案

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

Do not forget to use :

Imports System.Net.Mail
Imports System.Net

这篇关于邮件发送失败。无法连接到远程服务器。套接字操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆