This issue haunted me overnight as well. Here's how to fix it:
- Set host to: smtp.gmail.com
- Set port to: 587
This is the TLS Port. I had been using all of the other SMTP ports with no success. If you set enableSsl = true
like this:
Dim SMTP As New SmtpClient(HOST)
SMTP.EnableSsl = True
Trim the username and password fields (good way to prevent errors if user inputs the email and password upon registering like mine does) like this:
SMTP.Credentials = New System.Net.NetworkCredential(EmailFrom.Trim(), EmailFromPassword.Trim())
Using the TLS Port will treat your SMTP as SMTPS allowing you to authenticate. I immediately got a warning from Google saying that my email was blocking an app that has security risks or is outdated. I proceeded to "Turn on less secure apps". Then I updated the information about my phone number and google sent me a verification code via text. I entered it and voila!
I ran the application again and it was successful. I know this thread is old, but I scoured the net reading all the exceptions it was throwing and adding MsgBoxes after every line to see what went wrong. Here's my working code modified for readability as all of my variables are coming from MySQL Database:
Try
Dim MySubject As String = "Email Subject Line"
Dim MyMessageBody As String = "This is the email body."
Dim RecipientEmail As String = "[email protected]"
Dim SenderEmail As String = "[email protected]"
Dim SenderDisplayName As String = "FirstName LastName"
Dim SenderEmailPassword As String = "SenderPassword4Gmail"
Dim HOST = "smtp.gmail.com"
Dim PORT = "587" 'TLS Port
Dim mail As New MailMessage
mail.Subject = MySubject
mail.Body = MyMessageBody
mail.To.Add(RecipientEmail)
mail.From = New MailAddress(SenderEmail, SenderDisplayName)
Dim SMTP As New SmtpClient(HOST)
SMTP.EnableSsl = True
SMTP.Credentials = New System.Net.NetworkCredential(SenderEmail.Trim(), SenderEmailPassword.Trim())
SMTP.DeliveryMethod = SmtpDeliveryMethod.Network
SMTP.Port = PORT
SMTP.Send(mail)
MsgBox("Sent Message To : " & RecipientEmail, MsgBoxStyle.Information, "Sent!")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
I hope this code helps the OP, but also anyone like me arriving to the party late. Enjoy.