Arbitur Arbitur - 7 months ago 11
Vb.net Question

How do I send a gmail email in vb.net?

I want to send an email, but it gives me an error.

I have this code:

Sub sendMail(ByVal title As String, ByVal content As String)
Dim SmtpServer As New SmtpClient("smtp.gmail.com", 25)
SmtpServer.Credentials = New Net.NetworkCredential("name@gmail.com", "password")
Dim mail As New MailMessage("name@gmail.com", "name@gmail.com", title, content)
SmtpServer.Send(mail)
End Sub


I have a try catch which tries to call this method, it doesnt work so the catch runs and i get thes exception:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. b6sm3176487lae.0 - gsmtp
Why do I get this error? and how do I fix it?

Answer

Gmail uses SMTP over SSL on port 465.

Try doing:


  Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465)
  ...
  SmtpServer.EnableSsl = True
  ...