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
Try configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), works for me.
Since you are receiving an SmtpException, the SmtpClient.Send Method (MailMessage) documentation states that the following reasons could be the cause:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With