通过MailKit发送邮件时出错

时间:2017-07-29 08:22:41

标签: asp.net-core mailkit

当我尝试通过ASP.Net Core中的MailKit发送电子邮件时,我得到例外:

  

5.3.4 554 5.2.0 STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied;   由于消息的永久异常而无法处理消息   无法提交消息。 16.55847:6900,   17.43559:0000000060010000000000000000000000000000,20.521

代码发送电子邮件:

 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();
        }

请帮助解释异常的原因以及如何解决此问题。感谢。

2 个答案:

答案 0 :(得分:1)

SendAsDeniedException表示不允许您从domain.com发送电子邮件。它(fromAddress字段)必须为var fromAddress = "the_correct_and_real_email_address@outlook.com"。 以下是两个类似的问题:

答案 1 :(得分:0)

首先,您没有设置正确的电子邮件并通过。

,您必须像上面一样设置Credentials

 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
     };