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.
Emailsender.cs:
using Microsoft.Extensions.Options;
using RequireConfirmedEmail.Entities;
using System;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
///
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)
{
try
{
// 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
};
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.
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,
Sherry
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
29 Points
162 Posts
.Net Core Email Confirm Gmail smtp problems
May 20, 2020 12:21 AM|Norkle|LINK
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.
Emailsender.cs:
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)
{
try
{
// 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...
client.Send(mail);
}
catch (Exception ex)
{
// TODO: handle exception
throw new InvalidOperationException(ex.Message);
}
return Task.CompletedTask;
}
}
}
====================appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=111.111.111.11;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
Ned
All-Star
48740 Points
18193 Posts
Re: .Net Core Email Confirm Gmail smtp problems
May 20, 2020 10:43 AM|PatriceSc|LINK
Hi,
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
Contributor
2070 Points
606 Posts
Re: .Net Core Email Confirm Gmail smtp problems
May 21, 2020 04:42 AM|Sherry Chen|LINK
Hi Norkle ,
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,
Sherry
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.