Error sending email in vb.net 2010

User7291 picture User7291 · Sep 13, 2013 · Viewed 7.3k times · Source

I have to following code:

Try
    Dim mail As New MailMessage()
    Dim SmtpServer As New SmtpClient("smtp.gmail.com")
    mail.From = New MailAddress(txtid.Text)
    mail.[To].Add(TextBox1.Text)
    mail.Subject = txtsub.Text
    mail.Body = txtmess.Text
    ' mail.Attachments.Add(New Attachment(OpenFileDialog1.FileName))
    SmtpServer.Port = 587
    SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text, txtpass.Text)
    SmtpServer.EnableSsl = True

    SmtpServer.Send(mail)
    MsgBox("E-mail Has Been Send Successfully !")
Catch ex As Exception
    MsgBox(ex.Message)
End Try

on the step

SmtpServer.Send(mail)

I'm always getting an error : Failure sending mail

Any idea how to fix it?

Note that I'm using VB.NET

Answer

Jason Hughes picture Jason Hughes · Sep 13, 2013

Try configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), works for me.