0
votes

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("[email protected]")
            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("[email protected]", "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 ...

2
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
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
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
@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
for ssl gmail uses 465 - David Wilson

2 Answers

2
votes

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("[email protected]", "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

0
votes

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.