提问人:AxD 提问时间:9/27/2023 更新时间:9/27/2023 访问量:24
身份验证失败时 MailKit 日志为空
MailKit log is empty on authentication fail
问:
以下代码在处理后失败并出现错误:AuthenticateAsync()
MailKit.Security.AuthenticationException: 535: Authentication failed.
Restarting authentication process. ---> MailKit.Net.Smtp.SmtpCommandException:
Authentication failed. Restarting authentication process.
public async Task SendMailMessage(M.MailMessage message)
{
using MemoryStream stream = new MemoryStream(10_240);
using StreamReader reader = new StreamReader(stream);
using ProtocolLogger logger = new ProtocolLogger(stream);
using SmtpClient smtp = new SmtpClient(logger);
try
{
await smtp.ConnectAsync(
_config.HostName,
_config.Port,
_config.SecurityType == EMailSecurityType.StartTls ? SecureSocketOptions.StartTlsWhenAvailable : SecureSocketOptions.Auto
);
await smtp.AuthenticateAsync(_config.UserName, _config.Password);
await smtp.SendAsync(new MimeMessage(message));
await smtp.DisconnectAsync(true);
}
catch
{
stream.Flush();
Debug.Print(reader.ReadToEnd());
}
}
为什么日志是空的?
答:
1赞
jstedfast
9/27/2023
#1
在从中读取流之前,您需要倒带流,否则当您调用 ReadToEnd() 时,您的位置位于流的末尾,这显然会返回一个空字符串。
评论
0赞
AxD
9/27/2023
哦,亲爱的。多谢!👍
评论