Failure Email to 535: 5.7.8 Username and Password not accepted

Priya
Priya
Participant
936 Points
28 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)
        {
            try
            {

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

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

                    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": "smtp.gmail.com",
    "Port": 587,
    "SenderName": "",
    "SenderEmail": "",
    "UserName": "",
    "Password": ""
  }

For security reason credential field kept blank here:^.

 

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

Share:   fb twitter linkedin
Answers
chatGPT
chatGPT
Member
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
Smith
Smith
None
2568 Points
74 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):
    https://admin.google.com/ac/security/lsa
  2. Less Secure Apps (Inbox Side):
    https://myaccount.google.com/lesssecureapps?pli=1
Posted On: 14-Apr-2023 04:30
Worked for me
 - Priya  14-Apr-2023 04:31
 Log In to Chat