Failure Sending Mail. Unable to connect to remote

2019-09-18 04:24发布

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)

3条回答
Animai°情兽
2楼-- · 2019-09-18 05:05
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
查看更多
唯我独甜
3楼-- · 2019-09-18 05:06

Your code might not have any error. But this specific 'Failure Sending Mail' occurs if your Mail account not configured properly. You must allow access to third party or external application to use your mail's SMTP service. You can change that in Mail setting.

Change that and try. That might work.

查看更多
我想做一个坏孩纸
4楼-- · 2019-09-18 05:22

My 2 cents. In my case I had everything laid out perfectly and what threw me off without realizing that I had privateinternetaccess application running in the background that prevented every attempt to connect to the server. Not cool :(

查看更多
登录 后发表回答