Search code examples
vb.netemailsmtpgmail

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("[email protected]", "password")
    Dim mail As New MailMessage("[email protected]", "[email protected]", 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?


Solution

  • Gmail uses SMTP over SSL on port 465.

    Try doing:

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