MailKit 的 SendAsync() SmtpCommandException
MailKit's SendAsync() SmtpCommandException
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@live.com"));
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@live.com", "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
进一步查找提供"details"的错误:
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]
该异常表示 SMTP 服务器拒绝您的邮件。
要获取更多详细信息,您可以按照以下说明获取协议日志:https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog
经过深思熟虑和足智多谋的搜索后,我通过添加另一个 NuGet 包 - NETCore.MailKit
设法解决了问题 "solved"。奇怪的是,它似乎工作得很好,它的 IEmailService
包含已经内置的发送电子邮件的方法(除非您需要进一步编辑)。
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@live.com"));
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@live.com", "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
进一步查找提供"details"的错误:
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]
该异常表示 SMTP 服务器拒绝您的邮件。
要获取更多详细信息,您可以按照以下说明获取协议日志:https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog
经过深思熟虑和足智多谋的搜索后,我通过添加另一个 NuGet 包 - NETCore.MailKit
设法解决了问题 "solved"。奇怪的是,它似乎工作得很好,它的 IEmailService
包含已经内置的发送电子邮件的方法(除非您需要进一步编辑)。