Smtp error while send mail with gmail setting

aravind aravind 6 Reputation points
2022-08-24T06:57:26.357+00:00

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  
        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.

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

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 29,916 Reputation points Microsoft Vendor
    2022-08-24T09:29:36.297+00:00

    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
    2022-11-25T15:07:41.6+00:00

    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 (SqlWork.com) 68,081 Reputation points
    2022-11-28T17:44:34.953+00:00
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.