1

I have tried different port numbers but I keep getting the error message and no email sent. I aim to be able to send the email successfully and the message will contain details from an appointment booked. Any help please?

    Dim UserName As String = "example@gmail.com"
    Dim mail As MailMessage = New MailMessage

    mail.From = New MailAddress(UserName)
    mail.To.Add(New MailAddress(txtEmailAddress.Text))
    mail.Subject = "Appointment Details"
    mail.Body = "Test message"

    mail.IsBodyHtml = True

    Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 465)
    client.EnableSsl = True
    client.Credentials = New System.Net.NetworkCredential(UserName, "***")
    Try
        client.Send(mail)
    Catch ex As Exception
        MessageBox.Show("Sending email failed. Please Try again")
    End Try
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Also, ***never** catch `System.Exception`*, catch a more specific subclass instead. – Dai Mar 10 '16 at 22:26
  • Hi, and welcome to Stack Overflow! Please include the error message, and possibly the stack trace, in your question so that we can help you identify the problem. – Visual Vincent Mar 10 '16 at 22:28
  • When you are developing your code, do yourself a favour and find out what the error is: `MessageBox.Show("Sending email failed. Please Try again. Error message: " & ex.Message)`. – Andrew Morton Mar 10 '16 at 22:30
  • The error message was: the SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. –  Mar 10 '16 at 22:37
  • When I used port 465. It simply says failure sending email. Error message above is for port 25 –  Mar 10 '16 at 22:39
  • Have you tried using an ordinary email client with the same credentials? It is possible that the account has been blocked by gmail. – Andrew Morton Mar 10 '16 at 22:55
  • I don't think it has been blocked. I created this account roughly 2 days ago specifically for my project –  Mar 10 '16 at 22:57
  • try adding `client.UseDefaultCredentials = False` just below the client declaration. If it is set to true, the credentials of the currently logged on windows user will be sent. If it is set to false then your credentials will be sent instead – David Wilson Mar 10 '16 at 23:07
  • Possible duplicate of [Fail Sending Email Unable To Connect the Remote Server](http://stackoverflow.com/questions/23360415/fail-sending-email-unable-to-connect-the-remote-server) – Trevor Mar 10 '16 at 23:45
  • @Drilzone this has been asked numerous times here before. Please see my link above, if that doesn't help please see the link in that post. – Trevor Mar 10 '16 at 23:48

2 Answers2

0

What you need to do is add client.UseDefaultCredentials = False, but make sure you set it to false before calling client.Credentials = New System.Net.NetworkCredential(UserName, "***")

Also, for GMail, you need to use port 587 which is the port that supports the STARTTLS extension (which is the only way that System.Net.Mail.SmtpClient supports SSL).

In other words, change your code to this:

Dim UserName As String = "example@gmail.com"
Dim mail As MailMessage = New MailMessage

mail.From = New MailAddress(UserName)
mail.To.Add(New MailAddress(txtEmailAddress.Text))
mail.Subject = "Appointment Details"
mail.Body = "Test message"

mail.IsBodyHtml = True

Dim client As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Credentials = New System.Net.NetworkCredential(UserName, "***")
Try
    client.Send(mail)
Catch ex As Exception
    MessageBox.Show("Sending email failed. Please Try again")
End Try
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Cheers, I'll test it now. Furthermore, where it says mail.body... would I be able to place, for example txtForename.Text, and the email will send the entered data? –  Mar 10 '16 at 23:11
  • hmmm. I attempted to send an email and it hasn't sent yet the error message hasnt appeared. I think the issue could do with security settings. –  Mar 10 '16 at 23:32
  • If you didn't get an exception, then the messages was sent. It just may not have arrived yet or perhaps it got filtered into a spam folder. – jstedfast Mar 10 '16 at 23:34
  • @jstedfast `If you didn't get an exception, then the messages was sent`, not entirely true. It doesn't mean it was sent at all it usually means the mail server excepted it, but doesn't mean it will be sent as other issues could arise. Usually when accepted it will sit in a mail queue waiting it's turn to get sent out. The end user for example may call send and then nothing else happens. Well he misstyped the recipients address, the mail server can't send it so it pushes it aside and comes back another time, but not in all cases... The user is thought to believe it did send but it never did... – Trevor Mar 11 '16 at 06:33
  • @Codexer yes, I know that. What I meant was that the message was sent to the SMTP server without error. At this point either the message will be delivered to the recipient(s) or he'll receive a bounce message(s). – jstedfast Mar 11 '16 at 11:59
  • In regards to exceptions, I would suggest subscribing to SendCompleted event and see if there were any errors via e.Error in SendCompleted. See a simple example here http://www.systemnetmail.com/faq/4.6.aspx – Karen Payne Mar 11 '16 at 16:48
0

Try this,

go to your gmail account (www.gmail.com) login, and check that you have "enabled Pop for all mail". you can find this under the tab "Forwarding and POP/IMAP"

Also try switching the port number to 587

user1234433222
  • 976
  • 2
  • 10
  • 21