Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error sending email in vb.net 2010

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

like image 598
User7291 Avatar asked Feb 10 '26 15:02

User7291


2 Answers

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

like image 112
Jason Hughes Avatar answered Feb 14 '26 00:02

Jason Hughes


Since you are receiving an SmtpException, the SmtpClient.Send Method (MailMessage) documentation states that the following reasons could be the cause:

  • The connection to the SMTP server failed.
  • Authentication failed.
  • The operation timed out.
  • EnableSsl is set to true, but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.
  • EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

I would use Trim() with your username and password text values to remove any potential leading or trailing spaces, like this:

SmtpServer.Credentials = New System.Net.NetworkCredential(txtid.Text.Trim(), 
                                                          txtpass.Text,Trim())

I would also recommend forcing the DeliveryMethod to use the SMTP server, like this:

SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
like image 29
Karl Anderson Avatar answered Feb 13 '26 23:02

Karl Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!