.netazure-ad-msalmsal-angular

Office 365 - "535: 5.7.3 Authentication unsuccessful"


I am sending O365 mail using MSAL library. Generating access token in front-end(angular) and pass the token to back-end(.Net) for send mail. After that is throw an exception saying "535: 5.7.3 Authentication unsuccessful". But if I generate token in back-end and pass to the code which send mail it works properly.

try{ var email1 = new MimeMessage();

            email1.From.Add(MailboxAddress.Parse(request.UserEmail));

          email1.To.Add(MailboxAddress.Parse("chathura.ishan9@gmail.com"));
           
            email1.Subject = "Test Email Subject";

            email1.Body = new TextPart(TextFormat.Html) { Text = "<h1>Example HTML Message Body</h1>" };

            using (var client = new SmtpClient(new ProtocolLogger(Console.OpenStandardOutput())))
            {
                try
                {
                    client.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);

                    var oauth21 = new SaslMechanismOAuth2(request.UserEmail, request.Token);

                    client.Authenticate(oauth21);

                    client.Send(email1);

                    client.Disconnect(true);

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex);
                    throw;

                }

            }

Solution

  • This has nothing to do with Angular's MSAL libraries. The problem is that your Office 365 account's SMTP authentication is disabled. To authenticate your Office 365 account, the third-party application employs SMTP submission. You can use the instructions below to activate SMTP authentication. Authenticated client SMTP submission (SMTP AUTH) can be enabled or disabled in Exchange Online.

    Learn more here: https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/authenticated-client-smtp-submission