0

I got an old application that is using aspnet membership controls changepassword and createuserwizard etc. These send emails in the end, via gmail, after sending the email successfully, I get an application_error

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

The requirement is I can go back to the normal flow of application and ignore this error. Note that the email is being sent successfully to the client. There is no exception within the procedure/catch.

Protected Sub ChangePwd_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles ChangePwd.SendingMail
Dim obj As SecurePassword = New SecurePassword()

    Select Case ConfigurationSettings.AppSettings("WhichSMTP")
        Case "gmail"
            
            Try
                obj.SendViaGmail(e.Message.Subject, e.Message.Body, e.Message.To.ToString())
            Catch ex As Exception
                Response.Write("Error sending email via gmail smtp: " & ex.Message)
            End Try
        End Select
End Sub
    

SecurePassword.vb

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

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

        Dim mailMessage As MailMessage = New MailMessage With {
        .From = New 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 got the above code from here

few things to note,

  1. I am running this code from my development machine (IISExpress).

  2. previously there was no code written under

    Case "gmail"

Update:

I have tried catching it in page_error, it actually comes here but it shows me an empty page now instead of successtemplate from changepassword control

Private Sub Page_Error(sender As Object, e As EventArgs) Handles Me.Error
    Dim exc As Exception = Server.GetLastError()

    ' Handle specific exception.
    If exc.GetType = GetType(SmtpException) Then

        ChangePwd.SuccessTemplate.InstantiateIn(ChangePassword)
        'ChangePwd.ChangePasswordTemplateContainer.Visible = False
    End If

    ' Clear the error from the server.
    Server.ClearError()
End Sub
Samra
  • 1,815
  • 4
  • 35
  • 71

0 Answers0