Jackie Yong
Jackie Yong

Reputation: 9

"The SMTP server requires a secure connection or the client was not authenticated"

Unable to solve the problem by enabling the google access to less secure apps and two-step verification

I have enabled the google access to less secure apps but I'm still encountering the issue.. I have even tried to enable two-step verification, but it couldn't work as well..


Protected Sub sendMail_Click(sender As Object, e As EventArgs) Handles sendMail.Click
        Dim mainconn As String = System.Configuration.ConfigurationManager.ConnectionStrings("user_ConnectionString").ConnectionString
        Dim con As New SqlConnection(mainconn)

        Dim query As String = "Select email, password from user_information where email = @email"
        Dim cmd As New SqlCommand
        cmd = New SqlCommand(query, con)

        cmd.Parameters.AddWithValue("@email", txtBoxEmailSupport.Text.Trim)
        con.Open()
        Dim sdr As SqlDataReader = cmd.ExecuteReader()


        If sdr.Read() Then

            Dim email_password As String = sdr("email").ToString.Trim
            Dim password_reset As String = sdr("password").ToString.Trim

            Dim mm As New MailMessage("[email protected]", txtBoxEmailSupport.Text.Trim)
            mm.Subject = "Your forgotten Password!"
            mm.Body = String.Format("Smtp is working, man~~", email_password, password_reset)
            mm.IsBodyHtml = True
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
            Dim nc As New NetworkCredential("jackieyong36gmail.com", "12345")
            smtp.UseDefaultCredentials = True
            smtp.Credentials = nc
            smtp.Port = 587
            smtp.Send(mm)

            lblStatus.Text = "Your password has been sent to " + txtBoxEmailSupport.Text

        Else
            lblStatus.Text = "This email was not existed!"

        End If

Upvotes: 0

Views: 247

Answers (1)

Peter Ksenak
Peter Ksenak

Reputation: 315

I already solved this problem with auth, this code works for me, download MailKit from NuGet and add to your references.. you need to enable only less secure apps.

Private Sub SendMail()
    Dim message = New MimeMessage()
    message.From.Add(New MailboxAddress("Mr. Kako Kak", "[email protected]"))
    message.[To].Add(New MailboxAddress("Mrs. Chanandler Bong", "[email protected]"))
    message.Subject = "subjectText"
    message.Body = New TextPart("plain") With {
        .Text = "bodyText"
    }

    Using client = New SmtpClient()
        client.Connect("smtp.gmail.com", 465)
        client.AuthenticationMechanisms.Remove("XOAUTH2")
        client.Authenticate("[email protected]", "yourgmailpswd")
        client.Send(message)
        client.Disconnect(True)
    End Using
End Sub

Upvotes: 2

Related Questions