0
votes

It's work before, but suddenly there was this problem last month. Without changing the code

In my issue is when i trying to send the email with SmtpClient In VB.net .It's throw exception error message as

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.

Whether I use app password / less secure app access also with same error!

Code of below :

Using client As New SmtpClient(AccountOption.AccMailSMTP, AccountOption.AccMailPort) 'SMTP: smtp.gmail.com & Port: 587

    message = New MailMessage(ParamEmail, fileReaderEmail) 'From Email & To Email

        Dim rich As New RichEditControl
        Dim test As New MemoryStream(AccountOption.AccMailDefaultContent)

        rich.LoadDocument(test, DocumentFormat.Html)
        message.Attachments.Add(New Attachment(file))
        message.Subject = subject.Name & " For " & IO.Path.GetFileName(debtorFolder)
        message.IsBodyHtml = True
        message.Body = rich.HtmlText

        client.UseDefaultCredentials = True
        client.Credentials = New Net.NetworkCredential(ParamEmail, ParamPassword) 'To Email & The Password / App Password
        client.DeliveryMethod = SmtpDeliveryMethod.Network
        client.Timeout = 10000
        client.EnableSsl = True

        Try
            client.Send(message)
        Catch ex As Exception
            Throw ex
        End Try
End Using

The Link i have tried . But it same:

How do I send an email with Gmail and SmtpClient when the sending account uses two factor authentication?

The server response was: 5.7.0 Authentication Required. Learn more at

1
DO NOT post pictures of code. We can't copy text from pictures. Post your code as text and format it as code. - jmcilhinney
Hi @jmcilhinney, I have changed the picture to text . - Yang
I don't know the answer to your question as I never send email using Gmail but I do have a couple of comments about your code. Firstly, don't ever use Throw ex. That will truncate the stack trace on the exception. If you want to rethrow an exception then use just Throw. That said, if you really do nothing but rethrow the exception in a Catch block (which you may not if this code is simplified) then don't bother with exception handling at all. Secondly, if you create an Attachment object then you should be disposing it after the email has been sent. - jmcilhinney
@jmcilhinney Thank for your comments , I will enhance it after . - Yang
check my answer here : stackoverflow.com/questions/58686992/… ... maybe it will be usefull.. it works for me - Peter Ksenak

1 Answers

0
votes

hi go to your google account in the security panel enter image description here

Click on turn off/on access (recommend)

in the opened panel it must be turned on

enter image description here