Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am using Gmail setting for sending mail, i got an error like "Failure sending mail." and InnerException as "Unable to read data from the transport connection: net_io_connectionclosed."

My project frame work is .Net 4.8
Programming Language vb
OS is Windows 11

Anything need to change in Gmail account setting ?

What I have tried:

VB.NET
Imports System.Net
Imports System.Net.Mail
Public Class Form1
    Public Shared Function ServerCertificateValidationCallback(ByVal sender As Object, ByVal cert As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function
   
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault Or SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or SecurityProtocolType.Tls12
          
            Dim Mail As New System.Net.Mail.MailMessage
            Dim smtp As New System.Net.Mail.SmtpClient
            Mail.From = New MailAddress("aravind@gmail.com", "Aravind")
            Mail.To.Add("aravind1@gmail.com")
            Mail.IsBodyHtml = True
            Mail.Subject = "Hai Testing Mail"
            smtp = New SmtpClient("smtp.gmail.com", "587")
            smtp.EnableSsl = True
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network
            smtp.UseDefaultCredentials = False
            smtp.Credentials = New System.Net.NetworkCredential("aravind@gmail.com", "xxxxxxxxxxx")
            Dim userstate As Object = Mail
            System.Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf ServerCertificateValidationCallback
            smtp.Send(Mail)
        Catch ex As Exception
            MessageBox.Show(ex.InnerException.ToString)
            MessageBox.Show(ex.Message.ToString)
        End Try
    End Sub
End Class


Same code with another gmail account working fine,
Note: i created app password from security tab, one account working another cant, both i enabled 2 step verification.
Posted
Updated 26-Aug-22 8:50am
v3

1 solution

Gmail uses port 465 for SSL on SMTP.

Outgoing mail (SMTP) server smtp.gmail.com

Requires SSL: Yes

Requires TLS: Yes (if available)

Requires Authentication: Yes

Port for SSL: 465

Port for TLS/STARTTLS: 587

Aslso you Dim the smtp as a new smtpclient then assign it later with a new smtpcliet, one or the other will do and you can initialise the smtpclient on creation : smtp = new smtpclient(Server, Port).

Microsoft recommend NOT using smtpclient but alternatives like MailKit instead.

SmtpClient Class (System.Net.Mail) | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900