Click here to Skip to main content
15,791,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi. I try create core MVC with .NET 7. I create class for send email to user for confirm their email. but I get this error:

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at
System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, string response)

how can I fix this?

What I have tried:

MynMessageSender class is:
public class MessageSender : IMessageSender

       private readonly IConfiguration _configuration;
       public MessageSender(IConfiguration configuration)
           _configuration = configuration;
       public Task SendEmailAsync(string toEmail, string subject, string message, bool isMessagehtml = false)

           using (var client = new SmtpClient())
               var credentails = new NetworkCredential()
                   UserName = _configuration.GetValue<string>("websiteGmail:Mail"), // withiout
                   Password = _configuration.GetValue<string>("websiteGmail:Password")
               client.Credentials = credentails;
               client.Host = "";
               client.Port = 587;
               client.EnableSsl = true;

               using var emailMesage = new MailMessage()
                   To = { new MailAddress(toEmail) },
                   From = new MailAddress(_configuration.GetValue<string>("websiteGmail:FullEmailAddress")!),
                   Subject = subject,
                   Body = message,
                   IsBodyHtml = isMessagehtml
           return Task.CompletedTask;

and in register action after user create account successfully my code is:

await userManager.AddToRoleAsync(user, "User");

                   var emailConfirmationToken = await userManager.GenerateEmailConfirmationTokenAsync(user);
                   var emailMessage =
                       Url.Action("ConfrimEmail", "Sign", new {username = user.UserName,token = emailConfirmationToken},

                   await _messageSender.SendEmailAsync(user.Email, "Email Confirmation" , emailMessage);

1 solution

With GMail, you don't use your normal account and password. You need to generate a "special" app paasword for the email address that you want to use. Please read this: Sign in with app passwords - Gmail Help[^]

Here is a quick'n'dirty (tested & working) .Net 7.0 console app to test with:
using System.Diagnostics;
using System.Net.Mail;
using System.Net;

var smtpClient = new SmtpClient("")
    Port = 587,
    Credentials = new NetworkCredential(
        "[gmail email address]",
        "[generated app password]"),
    EnableSsl = true,

        from: "[gmail email address]",
        recipients: "[one or more recipient email addresses]",
        subject: "Test message",
        body: "This is a test message sent from .Net 7.0 application");

catch (SmtpException ex)

Share this answer
Member 11400059 25-Sep-23 8:22am    
thank you. now I can send the email to user.
Graeme_Grant 25-Sep-23 8:54am    
You are welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900