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)
Do not forget to use :
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.
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 :(