我正在使用 MSAL 库发送 O365 邮件。在前端(角度)生成访问令牌并将令牌传递给后端(.Net)以发送邮件。之后抛出异常“535: 5.7.3 Authentication unsuccessful”。但是,如果我在后端生成令牌并传递给发送邮件的代码,它就可以正常工作。
尝试{ 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;
}
}