无法使用SmtpClient& ;;在Outlook 365上发送电子邮件.Net 4.x [英] Unable to send email on Outlook 365 using SmtpClient & .Net 4.x
问题描述
我今天写的,因为我有一个与SMTP有关的错误,这让我感到磕磕绊绊。
事实上,当我尝试使用.NET 4.x运行时支持的System.Net.Mail(或其他外部邮件插件)在office365 SMTP服务器上发送邮件时,我得到该错误:
System.Net.Mail.SmtpException:无法发送消息。 ---> System.AggregateException:发生一个或多个错误。 ---> System.Security.Authentication.AuthenticationException:对SSPI的调用失败,请参阅内部异常。
但是,当我在.NET 3.x支持中设置我的项目时,该错误消失了。
同样,当我尝试另一台SMTP服务器作为Gmail服务器时,它与4.x支持完美配合。
这是我的代码:
void SendMailTest()
{
try
{
MailMessage mail = new MailMessage();
#region MailLayout
mail.From = new System.Net.Mail.MailAddress("myadress");
mail.To.Add("youradress");
mail.Subject = "test_sub";
mail.Body = "test_body";
mail.Attachments.Add(new Attachment("myPDF"));
#endregion
#region SMTPConfig
SmtpClient smtpServer = new SmtpClient("smtp.office365.com");
smtpServer.Port = 587;
smtpServer.Credentials = new NetworkCredential("myAdress", "myPassword") as ICredentialsByHost;
smtpServer.EnableSsl = true;
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{ return true; };
#endregion
smtpServer.Send(mail);
}
catch (Exception e){ Debug.Log(e.Message); }
}
感谢您阅读,我希望有人会帮助m e。
推荐答案
AggregateException是一个或多个实际错误的包装器。使用InnerExceptions属性来获取发生的错误。
An AggregateException is a wrapper around one or more actual errors. Use the InnerExceptions property to get to the error(s) that occurred.
这篇关于无法使用SmtpClient& ;;在Outlook 365上发送电子邮件.Net 4.x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!