.Net Core Email Confirm Gmail smtp problems RRS feed

  • Question

  • User-2006371901 posted

    I have a .net core 2.2 app with identity package and email confirmation required for users who register. I was able to have this send successfully after configuring my gmail to "allow less secure apps" . Now I am receiving error :"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required " when new user registers. I have a class called emailsender that draws from appsettings.json file. 


    using Microsoft.Extensions.Options;
    using RequireConfirmedEmail.Entities;
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Threading.Tasks;

    namespace RequireConfirmedEmail.Services
    public interface IEmailSender
    Task SendEmailAsync(string email, string subject, string htmlMessage);

    public class EmailSender : IEmailSender
    private readonly EmailSettings _emailSettings;

    public EmailSender(IOptions<EmailSettings> emailSettings)
    _emailSettings = emailSettings.Value;

    public Task SendEmailAsync(string email, string subject, string message)
    // Credentials
    var credentials = new NetworkCredential(_emailSettings.Sender, _emailSettings.Password);

    // Mail message
    var mail = new MailMessage()
    From = new MailAddress(_emailSettings.Sender, _emailSettings.SenderName),
    Subject = subject,
    Body = message,
    IsBodyHtml = true

    mail.To.Add(new MailAddress(email));

    // Smtp client
    var client = new SmtpClient()
    Port = _emailSettings.MailPort,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Host = _emailSettings.MailServer,
    EnableSsl = true,
    Credentials = credentials

    // Send it...
    catch (Exception ex)
    // TODO: handle exception
    throw new InvalidOperationException(ex.Message);

    return Task.CompletedTask;


    "ConnectionStrings": {
    "DefaultConnection": "Data Source=;initial catalog=mydb;persist security info=True;user id=myusername;password=yyy;"
    "Logging": {
    "LogLevel": {
    "Default": "Warning"
    "AllowedHosts": "*",
    "EmailSettings": {
    "MailServer": "smtp.gmail.com",
    "MailPort": 587,
    "SenderName": "My Company",
    "Sender": "mymail@gmail.com",

    "Password": "zzz"


    My gmail is configured for "allow less secure apps" , takes 587 for port via ssl 

    thanks in advance


    Wednesday, May 20, 2020 12:21 AM

All replies

  • User753101303 posted


    You tried to log to Gmail to see if you have a warning message?

    It can happen when deploying to a remote server. if Gmail sees a connection which is done from too far, too soon, it is handled as an hacking attempt. It should send a message about this and you can disallow that as well if needed.

    Not directly related but you may want to consider at somepoint using https://github.com/jstedfast/MailKit which is is AFAIK able to handles the "modern GMail" authentication option.

    Edit: this suggested at https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netcore-2.2#remarks

    Wednesday, May 20, 2020 10:43 AM
  • User-854763662 posted

    Hi Norkle ,


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

    Check if the username and password are correct. And check whether the network status is good, the cause of the error may also be caused by connection timeout.

     Refer to this answer which may be helpful. Perhaps, you can find something useful on this System.Net.Mail FAQ too.

    Best Regards,


    Thursday, May 21, 2020 4:42 AM