You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Server 2008 R2上自制.exe无法运行问题求助

问题描述

近5天来,在Windows 10系统的Visual Studio Code中编译生成的.exe文件(分别用Python、VB、C#实现),部署到Windows Server 2008 R2 Enterprise服务器后均无法运行。其中C#版本的功能是从指定文件夹提取4个Excel文件并发送至指定邮箱,已尝试调整兼容性配置、更换邮件服务商,问题仍未解决。

C#实现代码如下:

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;

namespace EmailSender
{
    class Program
    {
        static void Main(string[] args)
        {
            // Datos de autenticación del correo electrónico
            string smtpServer = "smtp.office365.com";
            int smtpPort = 587;
            string smtpUsername = "example@outlook.com";
            string smtpPassword = "example";

            string rutaCarpeta = "C:/Example";

            string[] archivosExcel = Directory.GetFiles(rutaCarpeta, "*.xls")
                .OrderByDescending(f => new FileInfo(f).CreationTime)
                .Take(4)
                .ToArray();

            MailMessage correo = new MailMessage();
            correo.From = new MailAddress("example@outlook.com");
            correo.Subject = "Example subject";
            correo.To.Add("example@gmail.com");
            correo.To.Add("example2@gmail.com");
            correo.Body = "Example body.";

            foreach (string archivo in archivosExcel)
            {
                correo.Attachments.Add(new Attachment(archivo));
            }

            SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort);
            smtpClient.EnableSsl = true;
            smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword);

            smtpClient.Send(correo);

            Console.WriteLine("Example writeline.");
        }
    }
}
排查与解决方案

1. 检查.NET Framework版本兼容性

Windows Server 2008 R2默认仅支持.NET Framework 4.0及4.5版本,若你在VS Code中使用了.NET 6+等更高版本编译,服务器缺少对应运行时会导致程序无法启动。

  • 解决:将项目目标框架改为.NET Framework 4.0/4.5,或在服务器上安装对应版本的.NET运行时。

2. 添加异常捕获,获取具体错误信息

当前代码无异常处理逻辑,运行出错后不会输出任何提示,无法定位问题。修改代码添加异常捕获,将错误信息写入日志文件:

using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;

namespace EmailSender
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // 原业务代码保持不变
                string smtpServer = "smtp.office365.com";
                int smtpPort = 587;
                string smtpUsername = "example@outlook.com";
                string smtpPassword = "example";

                string rutaCarpeta = "C:/Example";

                string[] archivosExcel = Directory.GetFiles(rutaCarpeta, "*.xls")
                    .OrderByDescending(f => new FileInfo(f).CreationTime)
                    .Take(4)
                    .ToArray();

                MailMessage correo = new MailMessage();
                correo.From = new MailAddress("example@outlook.com");
                correo.Subject = "Example subject";
                correo.To.Add("example@gmail.com");
                correo.To.Add("example2@gmail.com");
                correo.Body = "Example body.";

                foreach (string archivo in archivosExcel)
                {
                    correo.Attachments.Add(new Attachment(archivo));
                }

                SmtpClient smtpClient = new SmtpClient(smtpServer, smtpPort);
                smtpClient.EnableSsl = true;
                smtpClient.Credentials = new NetworkCredential(smtpUsername, smtpPassword);

                smtpClient.Send(correo);

                Console.WriteLine("Example writeline.");
                File.WriteAllText("C:/EmailSender_SuccessLog.txt", $"执行成功,时间:{DateTime.Now}");
            }
            catch (Exception ex)
            {
                string errorContent = $"执行失败,时间:{DateTime.Now}\n错误信息:{ex.Message}\n堆栈跟踪:{ex.StackTrace}";
                Console.WriteLine(errorContent);
                File.WriteAllText("C:/EmailSender_ErrorLog.txt", errorContent);
            }
        }
    }
}

重新编译后在服务器运行,查看生成的日志文件即可明确错误原因。

3. 验证文件与权限

  • 确认服务器上C:/Example文件夹存在,且运行.exe的账户拥有该文件夹的读取权限;
  • 尝试以管理员权限运行.exe,避免因权限不足导致的文件访问或网络操作失败。

4. 启用TLS 1.2支持

Windows Server 2008 R2默认未启用TLS 1.2,而smtp.office365.com要求使用TLS 1.2及以上版本连接,需手动开启:

  1. 打开注册表编辑器,定位到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols;
  2. 新建项TLS 1.2,在其下分别新建Client和Server子项;
  3. 在Client和Server子项中,各新建两个DWORD值:Enabled设为1,DisabledByDefault设为0;
  4. 重启服务器使设置生效。

5. 检查邮件账户配置

  • 若Office 365账户启用了多重身份验证(MFA),需使用应用密码代替普通登录密码;
  • 确认发件人邮箱未被Office 365限制SMTP发送权限。

内容的提问来源于stack exchange,提问作者Fernando Daniel Roth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 10:17:34