Smtp error while send mail with gmail setting

aravind aravind 6 Reputation points

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:

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  
            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")  
            Mail.IsBodyHtml = True  
            Mail.Subject = "Hai Testing Mail"  
            smtp = New SmtpClient("", "587")  
            smtp.EnableSsl = True  
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network  
            smtp.UseDefaultCredentials = False  
            smtp.Credentials = New System.Net.NetworkCredential("", "xxxxxxxxxxx")  
            Dim userstate As Object = Mail  
            System.Net.ServicePointManager.ServerCertificateValidationCallback = AddressOf ServerCertificateValidationCallback  
        Catch ex As Exception  
        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.

A set of technologies in the .NET Framework for building web applications and XML web services.
3,234 questions
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 25,156 Reputation points Microsoft Vendor

    Hi @aravind aravind ,
    You may need to change the "Less secure apps" setting on your Gmail account. Enable Ssl, use port 587 and enable "Less Secure Applications".
    You can also try deleting SecurityProtocolType.SystemDefault.
    Best regards,
    Lan Huang

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Johnny Molyneux 6 Reputation points

    I've been led to believe that as of May 2022 the "Less secure apps" has been disabled on gmail. Can anyone confirm this and if so, is there no longer a way to use Google's smtp server in C# apps?

    1 person found this answer helpful.
    0 comments No comments

  3. Bruce ( 54,621 Reputation points
    1 person found this answer helpful.
    0 comments No comments