-1

I am trying to send an email after a click of a button. But this is the exception i get

enter image description here

Is something wrong with my code ? I have already tried to go to my gmail settings and turned on the "Access for less secure apps" , still problem persists.

Imports System.Net.Mail 
Imports System.Net

Partial Class _Default
Inherits System.Web.UI.Page` 

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Try
        Dim mailMessage As New MailMessage("group2@gmail.com", "ndumisosizwe@gmail.com")
        mailMessage.Subject = "Mail Body"
        mailMessage.Body = "This is a test email"
        Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
        Dim credentials As New NetworkCredential("group2@gmail.com", "myPasswordHere")
        SmtpClient.Credentials = credentials

        SmtpClient.EnableSsl = True
        SmtpClient.Send(mailMessage)
        MsgBox("Email sent successfully")
    Catch ex As Exception
        MsgBox("Email Failed !  " & ex.ToString)
    End Try
End Sub
End Class

The exception is thrown on the line SmtpClient.Send(mailMessage)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ndumiso
  • 220
  • 4
  • 13

1 Answers1

0

The port you are using is for TLS according to this page. The port for SSL (older version) is 465.

Perhaps EnableSsl does not support TLS. You can try with that port.

Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207