Failure Email to 535: 5.7.8 Username and Password not accepted

1062 Points
29 Posts

I'm trying to use Smtp email sender to send email for testing purpose. But getting error:

Failure Email to 535: 5.7.8 Username and Password not accepted

Following are the code:

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Identity.UI.Services;
using SampleApi.Models.AppSettings;
using SampleApi.Models;
using MimeKit;
using MailKit.Net.Smtp;

namespace SampleApi.Bal
    public class EmailSender : IEmailSender
        private readonly ILogger _logger;
        private readonly AuthMessageSenderOptions _authMessageSenderOptions;
        private readonly MailAppSettings _mailAppSettings;
        public EmailSender(IOptions<AuthMessageSenderOptions> optionsAccessor,
                           ILogger<EmailSender> logger,
                           IOptions<MailAppSettings> mailAppSettings)
            _authMessageSenderOptions = optionsAccessor.Value;
            _logger = logger;
            _mailAppSettings = mailAppSettings.Value;

        public async Task SendEmailAsync(string toEmail, string subject, string message)
            await SendSMTPMailAsync(new MailData()
                EmailBody = message,
                EmailSubject = subject,
                EmailToId = toEmail

        public async Task<bool> SendSMTPMailAsync(MailData mailData)

                using (MimeMessage emailMessage = new MimeMessage())
                    MailboxAddress emailFrom = new MailboxAddress(_mailAppSettings.SenderName, _mailAppSettings.SenderEmail);
                    MailboxAddress emailTo = new MailboxAddress(mailData.EmailToName, mailData.EmailToId);

                    // you can add the CCs and BCCs here.
                    //emailMessage.Cc.Add(new MailboxAddress("Cc Receiver", ""));
                    //emailMessage.Bcc.Add(new MailboxAddress("Bcc Receiver", ""));

                    emailMessage.Subject = mailData.EmailSubject;

                    BodyBuilder emailBodyBuilder = new BodyBuilder();
                    emailBodyBuilder.TextBody = mailData.EmailBody;

                    emailMessage.Body = emailBodyBuilder.ToMessageBody();

                    //this is the SmtpClient from the Mailkit.Net.Smtp namespace, not the System.Net.Mail one
                    using (SmtpClient mailClient = new SmtpClient())
                        await mailClient.ConnectAsync(_mailAppSettings.Server, _mailAppSettings.Port);
                        await mailClient.AuthenticateAsync(_mailAppSettings.UserName, _mailAppSettings.Password);
                        await mailClient.SendAsync(emailMessage);
                        await mailClient.DisconnectAsync(true);

            catch (Exception ex)

                _logger.LogInformation($"Failure Email to {ex.Message}");

            return true;

in appsettings:

  "MailAppSettings": {
    "Server": "",
    "Port": 587,
    "SenderName": "",
    "SenderEmail": "",
    "UserName": "",
    "Password": ""

For security reason credential field kept blank here:^.


Views: 482
Total Answered: 2
Total Marked As Answer: 1
Posted On: 10-Apr-2023 04:59

Share:   fb twitter linkedin
92 Points
0 Posts

The error message "535: 5.7.8 Username and Password not accepted" typically indicates that there is an authentication problem when trying to access an email account through SMTP (Simple Mail Transfer Protocol). This error message may occur when the provided username and password for the email account are incorrect, or when the account is not configured to allow SMTP access.


There are several possible causes of this error, such as incorrect login credentials, a configuration issue, or a problem with the email server. To resolve this issue, the recipient should first verify that the username and password they are using are correct. If the login information is correct, they should check if the email account is properly configured to allow SMTP access.


If the problem persists, the recipient should contact their email service provider or system administrator for assistance in resolving the issue.

Posted On: 11-Apr-2023 02:03
2768 Points
76 Posts

For following error below when attempting to connect to the platform.

smtp error Error: Invalid login: 535-5.7.8 Username and Password not accepted.

It typically indicates that we did not enable Less Secure Apps on the inbox side of our email address. If we see this error, navigate to this 2 links below and double check that these settings are enabled, and try connecting again:

  1. Less Secure Apps (Admin Side):
  2. Less Secure Apps (Inbox Side):
Posted On: 14-Apr-2023 04:30
Worked for me
 - Priya  14-Apr-2023 04:31
 Log In to Chat