Office 365 no longer supports basic security (username and password). You will need to use oauth2. See this thread:
MailKit.Security.AuthenticationException: '535: 5.7.139 Authentication unsuccessful, the user credentials were incorrect.
I am getting this error "MailKit.Security.AuthenticationException: '535: 5.7.139 Authentication unsuccessful, the user credentials were incorrect." for my mail integration in my ASP.Net Core API Application.
I am using a Microsoft account to send the email, i tested it with PowerShell command and it is working but when i integrate it in my application it is not working.
I have disabled the security defaults and the account does not have any MFA and is excluded in conditional access policy for MFA
These are the settings i have set
"EmailSettings": {
"Host": "smtp.office365.com",
"Port": 587,
"Email": "Email ID",
"Password": "App Password",
"DisplayName": "Display Name",
"EnableSSL": "true"
}
And calling them like this in my code
await _emailService.SendAsync(dto.Email, "Subject", emailbody);
Email Service file:
public async Task SendAsync(string email, string subject, string body)
{
await SendEmailAsync(email, subject, body);
}
public async Task SendEmailAsync(string toEmail, string subject, string body)
{
var smtpHost = _config["EmailSettings:Host"];
var smtpPortString = _config["EmailSettings:Port"];
var smtpEmail = _config["EmailSettings:Email"];
var smtpPassword = _config["EmailSettings:Password"];
var displayName = _config["EmailSettings:DisplayName"];
var enableSSL = _config["EmailSettings:EnableSSL"];
if (string.IsNullOrEmpty(smtpHost) || string.IsNullOrEmpty(smtpPortString) ||
string.IsNullOrEmpty(smtpEmail) || string.IsNullOrEmpty(smtpPassword))
{
throw new ArgumentNullException("Missing required email configuration values.");
}
if (!int.TryParse(smtpPortString, out int smtpPort))
{
throw new ArgumentException("Invalid SMTP port number.");
}
bool useSSL = bool.TryParse(enableSSL, out var parsedSsl) && parsedSsl;
var secureSocketOption = useSSL ? SecureSocketOptions.StartTls : SecureSocketOptions.Auto;
var message = new MimeMessage();
message.From.Add(new MailboxAddress(displayName, smtpEmail));
message.To.Add(MailboxAddress.Parse(toEmail));
message.Subject = subject;
message.Body = new TextPart("html")
{
Text = body
};
try
{
using var client = new SmtpClient();
Console.WriteLine($"Connecting to SMTP server {smtpHost}:{smtpPort} with SSL: {useSSL}...");
await client.ConnectAsync(smtpHost, smtpPort, secureSocketOption);
await client.AuthenticateAsync(smtpEmail, smtpPassword);
await client.SendAsync(message);
await client.DisconnectAsync(true);
Console.WriteLine("Email sent successfully.");
}
catch (MailKit.Security.AuthenticationException authEx)
{
Console.WriteLine($"Authentication error: {authEx.Message}");
throw;
}
catch (Exception ex)
{
Console.WriteLine($"General error: {ex.Message}");
throw;
}
}
I am getting the error on this line : "await client.SendAsync(message);"
ASP.NET API
1 answer
Sort by: Most helpful
-
Bruce (SqlWork.com) 75,621 Reputation points Moderator
2025-05-08T16:02:06.66+00:00