简体   繁体   English

Asp.net MVC5 和 SMTP Office365 版本 4.5.2 出现异常,代码在版本 4.8 上运行良好

[英]Asp.net MVC5 with SMTP Office365 Exception at version 4.5.2, the code works well at version 4.8

在此处输入图像描述

I have an ASP.net MVC5 app that uses Office365 to send emails to users, that gives the following exception, the runtime version is 4.5.2我有一个 ASP.net MVC5 应用程序,它使用 Office365 向用户发送电子邮件,出现以下异常,运行时版本为 4.5.2

when I tried it in a new project with runtime version 4.8, it worked fine with no Errors, how to migrate this code to older runtime versions当我在运行时版本 4.8 的新项目中尝试它时,它运行良好,没有错误,如何将此代码迁移到较旧的运行时版本

public string SendEmail()
{
    MailAddress toAddress = new MailAddress("test2@test.com", "Test");
    const string subject = "Outlook Test";
    const string HtmlBody = "This is our outlook Test";
    SendConEmail(toAddress, subject, HtmlBody);
     return "Email successfully Sent";
}

public static void SendConEmail(MailAddress toAddress, string subject, string HtmlBody)
{
    MailAddress fromAddress = new MailAddress("test@test.com", "Test");
    const string fromPassword = "PAssword Here";
    var smtp = new SmtpClient
    {
        // Host = "smtp-mail.outlook.com",
        Host = "smtp.office365.com",
        Port = 587,
        EnableSsl = true,
        DeliveryMethod = SmtpDeliveryMethod.Network,
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
    };
    try
    {
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = HtmlBody,
            IsBodyHtml = true
        })
        {
            smtp.Send(message);
        }
    }
    catch (Exception ex)
    {
       Console.WriteLine(ex);
    }
}

Since you say the code works in 4.8 but not in 4.5.2, this is most likely a TLS problem.由于您说代码在 4.8 中有效,但在 4.5.2 中无效,这很可能是 TLS 问题。

Apps which target .NET Framework 4.7 or later automatically use the default security protocols defined by the operating system.针对 .NET Framework 4.7 或更高版本的应用程序会自动使用操作系统定义的默认安全协议。 Apps which target .NET Framework 4.5.2 require a registry setting , and potentially a hotfix , to do the same.以 .NET Framework 4.5.2 为目标的应用程序需要注册表设置,并且可能需要修补程序才能执行相同操作。

(The hotfix doesn't apply if the computer already has a later version of .NET Framework installed.) (如果计算机已经安装了更高版本的 .NET 框架,则此修补程序不适用。)

However, the simplest and recommended option is to upgrade your app to target at least .NET Framework 4.7.2, and preferably 4.8.但是,最简单和推荐的选项是升级您的应用程序以至少针对 .NET 框架 4.7.2,最好是 4.8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM