0

I am trying to generate an email function where it sends the user an email, however for some reason it is just not working properly and it does not give any error messages.

It just brings up a message box stating that "There is a problem at this time. Please try again later". This is message box which appears when the email fails to be sent, however could you please help to resolve by issue.

Error Message says

It says that 'sends the email. SMTP.Send(email)- An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at –

Please view the code below.

imports System.Net.Mail

Public Class EmailF
    'assigns a new message to the email
    Dim email As New MailMessage
    'smtp is the server that has been used to send the email
    Dim smtp As New SmtpClient
    'assigns a new email address
    Dim mail As System.Net.Mail.MailAddress


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            'selects which email address to send the email from...
            email.From = New MailAddress("blood4all.business@gmail.com")
            email.Subject = SubBox.Text
            email.Body = MessBox.Text
            email.To.Add(ToBox.Text)
            'the smtp outlook host.
            Dim SMTP As New SmtpClient("smtp.gmail.com")
            'the smtp port.
            SMTP.Port = 587
            SMTP.EnableSsl = True
            'the login details for the gmail account.
            SMTP.Credentials = New System.Net.NetworkCredential("blood4all.business@gmail.com", "Blood123")
            'starts the progress bar.
            Timer1.Start()
            ProgressB.Show()
            'sends the email.
            SMTP.Send(email)
            'pop up box conirms that the email has been sent.
            MsgBox("The e-mail has been sent!")
        Catch ex As Exception
            MsgBox("There is a problem at this time. Please try again later")
            ProgressB.Hide()
        End Try
    End Sub

The exception message is :

An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at ...

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
J.Doe
  • 59
  • 6
  • Comment out the line **Try**, and the line **Catch ex as exception** and the following three lines. Re-run the code and see what the error is. – David Wilson Dec 17 '15 at 10:40
  • It says that 'sends the email. SMTP.Send(email)- An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at – J.Doe Dec 17 '15 at 10:44
  • From @neil: The error message is generated by your code in the line: `MsgBox("There is a problem at this time. Please try again later")` Try replacing this with `MsgBox("Message: " & ex.Message & "StackTrace: " & ex.StackTrace)` to give a more descriptive error message. – Panagiotis Kanavos Dec 17 '15 at 10:46
  • @J.Doe please put the error message in the question itself. It's the *most* important information. – Panagiotis Kanavos Dec 17 '15 at 10:48
  • Try using port 465 - it might work – David Wilson Dec 17 '15 at 10:53
  • 2
    Just in case, but feel it needs to be said - if that's your _actual_ username/password, you may want to go change that password **now**. – James Thorpe Dec 17 '15 at 10:53
  • 1
    @DavidWilson please check the message, it's an authentication problem not a networking problem. Besides, GMail explicitly requires 587 and explains this quite clearly in its documentation – Panagiotis Kanavos Dec 17 '15 at 10:54
  • 1
    for ssl gmail uses 465 – David Wilson Dec 17 '15 at 10:56
  • Once changed it now says An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: Failure sending mail. – J.Doe Dec 17 '15 at 11:02
  • @DavidWilson *and* 587. This isn't an issue and you'll find that a lot of SO examples where people use 587. Besides, SSL is deprecated in favour of TLS and GMail could disable it any day. In any case, changing the port isn't necessary – Panagiotis Kanavos Dec 17 '15 at 11:14

2 Answers2

2

The credentials you pass are used only if you set the UseDefaultCredentials property to false before you set the new credentials. Otherwise your Windows credentials will be used.

Changing the value of this property clears the Credentials property, which is why you need to set it before passing the new credentials:

SMTP.UseDefaultCredentials = False
SMTP.Credentials = New System.Net.NetworkCredential("blood4all.business@gmail.com", "Blood123")

EDIT

You'll also have to "allow less secure applications" from your GMail account settings, as described in this SO question and the GMail support site. Normally, GMail requires applications to use either an application key.

SMTP (the protocol) doesn't have this functionality so you need to modify your account settings to allow connecting without an application key

Community
  • 1
  • 1
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Once I have typed SMTP.UseDefaultCredentiasl = False, it is not recgnosing this line as it says its not a member of System.Net.Mail.smtpclient. Please help and advise. – J.Doe Dec 17 '15 at 10:55
  • Sorry, it was a typo. Fixed – Panagiotis Kanavos Dec 17 '15 at 10:56
  • It says An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. – J.Doe Dec 17 '15 at 10:58
  • 1
    *Have* you enabled your account to be used by external applications? This is explained in [this SO question](http://stackoverflow.com/questions/31231033/using-smtpclient-to-send-an-email-from-gmail) and in the [GMail support site](https://support.google.com/accounts/answer/6010255). Typically, applications need to use an application key to work with GMail. SMTP (the protocol) doesn't have this functionality so you need to modify your account settings to allow connecting without an application key – Panagiotis Kanavos Dec 17 '15 at 11:16
0

I have now got this working as I created a new gmail account, maybe my all gmail account consisted of network and security issues which has now been amended. Thank for help and much appreciated.

J.Doe
  • 59
  • 6