When I try to send an email via MailKit
in ASP.Net Core, I get the exception:
5.3.4 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message Cannot submit message. 16.55847:69000000, 17.43559:0000000060010000000000000000000000000000, 20.521
Code send Email:
try
{
var fromAddress = "no-reply@domain.com";
var fromAdressTitle = "no-reply";
var toAddress = "mymail@outlook.com";
var toAdressTitle = "Test sending email";
var subject = "Hello World!";
var bodyContent = "content message";
var mimeMessage = new MimeMessage();
mimeMessage.From.Add(new MailboxAddress(fromAdressTitle, fromAddress));
mimeMessage.To.Add(new MailboxAddress(toAdressTitle, toAddress));
mimeMessage.Subject = subject;
mimeMessage.Body = new TextPart("plain")
{
Text = bodyContent
};
using (var client = new SmtpClient())
{
client.Connect("smtp-mail.outlook.com", 587,SecureSocketOptions.StartTls);
client.Authenticate("mymail@outlook.com", "mypass");
client.Send(mimeMessage);
Console.WriteLine("The mail has been sent successfully !!");
Console.ReadLine();
client.Disconnect(true);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
Please help explain the reason of exception and how to fix this issue. Thanks.