Home > Blockchain >  SmtpClient not working after setting up app password and 2-factor authentication enabled
SmtpClient not working after setting up app password and 2-factor authentication enabled

Time:12-16

My CreateUserWizard sends an email when I press a button. The email is send via app password as follows

Protected Sub NewUserWizard_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles NewUserWizard.SendingMail
    'Get the UserId of the just-added user
    Dim newUser As MembershipUser = Membership.GetUser(NewUserWizard.UserName)
    Dim newUserId As Guid = CType(newUser.ProviderUserKey, Guid)
    'some more code...

    Dim obj As SecurePassword = New SecurePassword()
    Dim google As GoogleOauth = New GoogleOauth()

    ' Replace <%VerificationUrl%> with the appropriate URL and querystring
    e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", fullUrl)
    Select Case ConfigurationSettings.AppSettings("WhichSMTP")
        Case "gmail"
            'no need to do anything
            Try
                obj.SendViaGmail(e.Message.Subject, e.Message.Body, e.Message.To.ToString())
            Catch ex As Exception
                Response.Write("Error sending email  " & ex.Message)
            End Try
      End Select
    End Sub

Secure.vb

 Public Function SendViaGmail(subject As String, body As String, recipients As String)
        
        Dim fromEmail As String = ConfigurationSettings.AppSettings("ContactEmail")

        Dim smtpClient As Mail.SmtpClient = New Mail.SmtpClient("smtp.gmail.com") With {
            .Port = 587,
            .DeliveryMethod = SmtpDeliveryMethod.Network,
            .Credentials = New NetworkCredential(fromEmail, "APP_PWD"), 
            .EnableSsl = True
            }

        Dim mailMessage As MailMessage = New MailMessage With {
        .From = New Mail.MailAddress(fromEmail),
        .Subject = subject,
        .Body = body,
        .IsBodyHtml = True
    }

        mailMessage.[To].Add(recipients)
        If smtpClient IsNot Nothing Then
            smtpClient.Send(mailMessage)
        End If
    End Function

I keep getting smtp server requires a secure connection or the client was not authenticated,...

also, I actually receive the email after which it just crashed the application. I have setup 2-factor authentication on my gmail account, generated app password and tried everything given on this post including re-ordering of credentials etc..nothing works!!!

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

UPDATE I have also just found a setting in web.config (I have replaced this password with app password) - result is the same

<system.net>
<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network defaultCredentials="false" enableSsl="true" host="smtp.gmail.com" port="587" userName="[email protected]" password="****" />
  </smtp>
</mailSettings>
</system.net> 

CodePudding user response:

First remove .DeliveryMethod = SmtpDeliveryMethod.Network, from your code, then make sure that the From Email address is the email address of the user that the apps password matches.

I have tested this and it works.

 Private Function SendViaGmail(subject As String, body As String, recipients As String) As String

        Dim smtpClient As Mail.SmtpClient = New Mail.SmtpClient("smtp.gmail.com") With {
                .Port = 587,
                .Credentials = New NetworkCredential(FromEmail, GoogleAppPassword), 
                .EnableSsl = True
                }

        Dim mailMessage As MailMessage = New MailMessage With {
                .From = New Mail.MailAddress(FromEmail),
                .Subject = subject,
                .Body = body,
                .IsBodyHtml = True
                }

        mailMessage.[To].Add(recipients)
        
        If smtpClient IsNot Nothing Then
            smtpClient.Send(mailMessage)
            Return "Message sent."
        End If
        
        Return "Error: failed to create smtpClient."
        
    End Function
  • Related