2020-12-31

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

I want to send an email from my application to verify the email that I used to register but getting this error.

The SMTP server requires a secure connection or the client was not authenticated. The server response 
was: 5.7.57 SMTP; 

I tried going into my Gmail settings and allow less secure apps On but it still doesn't work.

UserController.cs

[NonAction]
    public void SendVerificaitonLinkEmail(string email, string activationCode, string emailFor = "VerifyAccount")
    {
        var verifyUrl = "/User/" + emailFor + "/" + activationCode;
        var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, verifyUrl);

        var fromEmail = new MailAddress("dotnetawesome@gmail.com", "Dotnet Awesome");
        var toEmail = new MailAddress(email);
        var fromEmailPassword = "**********"; // Replace with actual password

        string subject = "";
        string body = "";
        if (emailFor == "VerifyAccount")
        {
            subject = "Your account is successfully created!";

            body = "<br/><br/>We are excited to tell you that your Rockstar Awesome account is" +
                " successfully created. Please click on the below link to verify your account" +
                " <br/><br/><a href ='" + link + "'>" + link + "</a> ";
        }
        else if (emailFor == "ResetPassword")
        {
            subject = "Reset Password";
            body = "Hi, <br/><br/> We got request for reset your account password. Please click on the link below to reset your password." +
                "<br/><br/><a href = " + link + ">Reset Password link </a>";
        }



        SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
        {
            client.EnableSsl = true;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(fromEmail.Address, fromEmailPassword);
        };

        using (var message = new MailMessage(fromEmail, toEmail)
        {
            Subject = subject,
            Body = body,
            IsBodyHtml = true
        })
            client.Send(message); // 
    }


from Recent Questions - Stack Overflow https://ift.tt/3o6EQ36
https://ift.tt/eA8V8J

No comments:

Post a Comment