Sas*_*sha 2 c# email mailkit asp.net-core
在asp.net core 1应用程序中,我正在尝试使用mailkit库(版本1.8.1)向strato服务器发送电子邮件.我的代码:
var emailMessage = new MimeKit.MimeMessage();
try
{
emailMessage.From.Add(new MimeKit.MailboxAddress("Support", "some@test.com"));
emailMessage.To.Add(new MimeKit.MailboxAddress("", "test@test.com"));
emailMessage.Subject = "Subject";
var bodyBuilder = new MimeKit.BodyBuilder();
bodyBuilder.HtmlBody = @"<b>Some body</b>";
emailMessage.Body = bodyBuilder.ToMessageBody();
using (var client = new MailKit.Net.Smtp.SmtpClient())
{
client.Timeout = 15000;
// Accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
await client.ConnectAsync("smtp.strato.de", 587, MailKit.Security.SecureSocketOptions.Auto);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
// Note: only needed if the SMTP server requires authentication
await client.AuthenticateAsync(login, passord);
await client.SendAsync(emailMesage);//here error!
await client.DisconnectAsync(true);
}
return true;
}
catch (System.Exception)
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试发送电子邮件时 ,尽管身份验证通过,我仍然获得5.7.0 User not authenticated错误.例如,使用gmail的相同代码可以正常工作.
任何想法如何解决它?
问题是当SMTP客户端在身份验证后发送EHLO命令时,smtp.strato.de会重置其身份验证状态,即使规范强烈建议客户端应该这样做.
换句话说,smtp.strato.de已被破坏.
也就是说,您可以通过执行以下操作来解决此问题:
client.QueryCapabilitiesAfterAuthenticating = false;
Run Code Online (Sandbox Code Playgroud)
只要在进行身份验证之前将该属性设置为false ,它就会起作用.
| 归档时间: |
|
| 查看次数: |
1446 次 |
| 最近记录: |