Search code examples
vb.netvisual-studio-2012smtpgmail

Failure Sending Mail. Unable to connect to remote server. A socket operation


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)

Solution

  • 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