1

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.

Soheil Alizadeh
  • 2,936
  • 11
  • 29
  • 56
m.shakeri
  • 1,203
  • 1
  • 12
  • 16

2 Answers2

3

SendAsDeniedException means it doesn't allow you to send an email from domain.com. It (the fromAddress field) must be var fromAddress = "the_correct_and_real_email_address@outlook.com". Here are 2 similar issues:

VahidN
  • 18,457
  • 8
  • 73
  • 117
  • 1
    That's not the reason, because when i send via "smtp.gmail.com" and the same fromAddress it work fine.but when i send via smtp-mail.outlook.com this is not work. – m.shakeri Jul 30 '17 at 04:53
  • This is the policy of Microsoft. You can't `send as` another person or domain from `outlook.com`. This is the meaning of `**Send As** Denied Exception`. – VahidN Jul 30 '17 at 05:18
  • I tested it.i set `fromAddress` to real Email Address but when sending Email an exception occur: `The SMTP server has unexpectedly disconnected.` – m.shakeri Jul 30 '17 at 07:04
0

First, you don't set correct email and pass.

and you must set Credentials like above:

 var credentials = new NetworkCredential("youemail@outlook.com","yourpass");
 var client = new SmtpClient(smtpEmail.smtp)
     {
         Port = 587,
         DeliveryMethod = SmtpDeliveryMethod.Network,
         UseDefaultCredentials = false,
         EnableSsl = true,
         Credentials = credentials
     };
Soheil Alizadeh
  • 2,936
  • 11
  • 29
  • 56