以下代码处理后失败
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());
}
}
为什么日志是空的?
在读取流之前,您需要倒回流,否则当您调用 ReadToEnd() 时,您的位置位于流的末尾,这显然会返回一个空字符串。