Cannot send email with VB.Net to Gmail

~OSD~ 2,201 Reputation points
2022-08-22T19:19:58.62+00:00
 Dim Mail As New MailMessage  
        Mail.Subject = "Sent with VB.net"  
        Mail.To.Add("******@gmail.com")  
        Mail.From = New MailAddress("******@gmail.com")  
        Mail.Body = "Hello from Visual Studio"  
        Dim SMTP As New SmtpClient("smtp.gmail.com")  
        SMTP.EnableSsl = True  
        SMTP.Credentials = New System.Net.NetworkCredential("******@gmail.com", "myPassword")  
        SMTP.Port = "587"  
        Dim dirAttach = "C:\MyDocs"  
        For Each h As String In IO.Directory.GetFiles(dirAttach , "*.jpg", IO.SearchOption.AllDirectories)  
            For Each t As String In IO.Directory.GetFiles(dirAttach , "*.txt", IO.SearchOption.AllDirectories)  
                Mail.Attachments.Add(New Attachment(h))  
                Mail.Attachments.Add(New Attachment(t))  
            Next  
        Next  
        SMTP.Send(Mail)  

The above code was working fine few months ago, but today it give me error that SMTP is not supported or it's not secure. Is something has changed or gmail is not supported any more?

Developer technologies VB
0 comments No comments
{count} vote

3 answers

Sort by: Most helpful
  1. Anonymous
    2022-08-23T21:17:46.897+00:00

    I just commented out the two for loops and using port 587 it works fine.

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    2 people found this answer helpful.

  2. Anonymous
    2022-08-23T01:10:21.933+00:00

    Try it with
    SMTP.Port = "465"

    1 person found this answer helpful.

  3. Anonymous
    2022-08-25T14:19:08.583+00:00

    Did you create an app password? I just created a new app password for my testing.
    https://support.google.com/accounts/answer/185833?hl=en

    Also the below worked fine for me as long as the directory exists

             Dim dirAttach = "C:\MyDocs"  
             For Each h As String In IO.Directory.GetFiles(dirAttach , "*.jpg", IO.SearchOption.AllDirectories)  
                Mail.Attachments.Add(New Attachment(h))  
             Next  
    	   For Each t As String In IO.Directory.GetFiles(dirAttach , "*.txt", IO.SearchOption.AllDirectories)  
                Mail.Attachments.Add(New Attachment(t))  
    	   Next  
    

    --please don't forget to upvote and Accept as answer if the reply is helpful--

    1 person found this answer helpful.

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.