Asp.Net Core 3 ssl发送邮件失败,解决方案:第三方组件Mailkit
1.博客园有提到:https://www.cnblogs.com/tsql/p/9078163.html ,这个是 .Net Freamwork的解决方案 ,当切换成core的情况 ,利用System.web.mail发送邮件方法不行(在core下面System.web这个命名空间类全部移除,若切换NetFreamwork到core代码也是重点切换内容)。
2.虽然 core下面 System.net.mail 是有的,但是和 .Net Freamwork版本同样的问题,ssl发送邮件失败。
3.最终采取方法是采用第三方组件Mailkit , 具体代码如下:
nuget 添加 :MailKit
using MailKit;
public static bool SendEmail(EmailAccount emailAccount, string toAddress, string subject, string body)
{
var mailAddress = emailAccount.MailAddress;
var message = new MimeMessage();
message.From.Add(new MailboxAddress(mailAddress));
foreach (var to in toAddress.Split(','))
{
message.To.Add(new MailboxAddress(to));
}
message.Subject = subject;
message.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text = body };
using (var client = new SmtpClient())
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect(emailAccount.Host, emailAccount.Port, emailAccount.EnableSsl);
// 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
client.Authenticate(mailAddress, emailAccount.Password);
client.Send(message);
client.Disconnect(true);
}
return true;
}
浙公网安备 33010602011771号