MailKit的SendAsync()SmtpCommandException

问题描述 投票:1回答:2

SmtpClient.SendAsync()方法似乎因未知原因而抛出异常,IDE甚至不显示异常。

public async Task SendEmailAsync(string From, string EmailFrom, string To, string EmailTo, string Subject, string Message)
        {
            var email = new MimeMessage();
            email.From.Add(new MailboxAddress(From, EmailFrom));
            email.To.Add(new MailboxAddress(To, "[email protected]"));
            email.Subject = Subject;
            email.Body = new TextPart("plain")
            {
                Text = Message
            };

            using (var Client = new SmtpClient())
            {
                Client.ServerCertificateValidationCallback =
                    (Sender, Certificate, ChainType, Errors) => true;
                Client.AuthenticationMechanisms.Remove("XOAUTH2");

                await Client.ConnectAsync("smtp.live.com", 587, SecureSocketOptions.StartTls).ConfigureAwait(false);
                //Client.Capabilities &= ~SmtpCapabilities.Pipelining;
                await Client.AuthenticateAsync("[email protected]", "PASSWORD").ConfigureAwait(false);

                await Client.SendAsync(email).ConfigureAwait(false);
                //SmtpCommandException is thrown in the line above, claiming "MessageNotAccepted"
                await Client.DisconnectAsync(true).ConfigureAwait(false);
            }
        }

我设法找到的是,在try-catch块中,SmtpCommandException被抛出IDE并没有将我重定向到它。内部异常是null,“ErrorCode”是MessageNotAccepted

它为我提供的唯一内容是堆栈跟踪:

at MailKit.Net.Smtp.SmtpClient.DataAsync(FormatOptions options, MimeMessage message, Boolean doAsync, CancellationToken cancellationToken, ITransferProgress progress)\r\n
at MailKit.Net.Smtp.SmtpClient.SendAsync(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsync, CancellationToken cancellationToken, ITransferProgress progress)\r\n
at MailKit.Net.Smtp.SmtpClient.SendAsync(FormatOptions options, MimeMessage message, MailboxAddress sender, IList`1 recipients, Boolean doAsync, CancellationToken cancellationToken, ITransferProgress progress)\r\n
at BashTheMovie.Services.MessageService.SendEmailAsync(String From, String EmailFrom, String To, String EmailTo, String Subject, String Message, Attachment[] Attachments) in ...Project\\Services\\File.cs:line 58

进一步查看提供错误的“详细信息”:

S: 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:BA030000, 17.43559:0000000094000000000000000000000000000000, 20.52176:140F568600004010F1030000, 20.50032:140F56867F174010F1030000, 0.35180:0D120000, 255.23226:00000000, 255.27962:0A000000, 255.27962:0E000000, 255.31418:140F5686, 16.55847:84000000, 17.43559:0000000070010000000000000200000000000000, 20.52176:140F56860000F01F0A000000, 20.50032:140F56867F1700110A0F0000, 0.35180:00000000, 255.23226:00000000, 255.27962:0A000000, 255.27962:32000000, 255.17082:DC040000, 0.27745:140F5686, 4.21921:DC040000, 255.27962:FA000000, 255.1494:68000000, 0.38698:0F010480, 0.37692:06000100, 0.37948:FE7F0300, 5.33852:00000000534D545000000100, 4.56248:DC040000, 7.40748:010000000000010B30303A56, 7.57132:00000000000000003A643965, 1.63016:32000000, 4.39640:DC040000, 8.45434:FE7F0300D9AD6053000000000000000000003932, 5.10786:0000000031352E32302E313632322E3030303A564931505230354D42333433383A64396532363039662D383037332D346338382D393863612D65336566633263646635383700333600000000, 255.1750:A4000000, 255.31418:0A005636, 0.22753:A9000000, 255.21817:DC040000, 4.60547:DC040000, 0.21966:03003866, 4.30158:DC040000 [Hostname=VI1PR05MB3438.eurprd05.prod.outlook.com]
c# asp.net-core mailkit
2个回答
1
投票

该异常意味着SMTP服务器拒绝您的消息。

要获取更多详细信息,可以按照以下说明获取协议日志:https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog


0
投票

经过一些深思熟虑和资源丰富的搜索,我设法通过添加另一个NuGet包 - NETCore.MailKit来解决问题。奇怪的是,它似乎工作得很好,它的IEmailService包含已经内置的发送电子邮件的方法(除非你需要进一步编辑)。

© www.soinside.com 2019 - 2024. All rights reserved.