通过powershell发送电子邮件不工作

本文关键字:工作 电子邮件 powershell 通过 | 更新日期: 2023-09-27 18:06:36

我正在编写一个脚本,通过powershell向人们发送电子邮件。它在一些电脑上可以正常发送,但在另一些电脑上根本不能发送。我知道两台电脑的配置不完全相同。

我不明白为什么。

我尝试在命令末尾添加2>> c:'error.txt,但它没有记录任何内容。

我的代码(出于保密原因我更改了smtp和email):

            File.Delete(@"C:'Temp'Email2Send.ps1");
            var strVar_EMAIL_BOX = "myemail@email.com";
            TextWriter tw = new StreamWriter(@"C:'Temp'Email2Send.ps1");
            tw.WriteLine("$OutputEncoding = [Console]::OutputEncoding");
            tw.WriteLine("$smtp = '"forwarder.mail.xxxxxx.com'"");
            tw.WriteLine("$to = '"" + To + "'"");
            tw.WriteLine("$from = '"" + strVar_EMAIL_BOX + "'"");
            tw.WriteLine("$enc = [System.Text.Encoding]::UTF8");
            tw.WriteLine("$subject = '"Suivi'"");
            tw.WriteLine("$body = '"" + Body + "'"");
            tw.WriteLine(@"send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Encoding $enc 2>> c:'errors.txt");
            tw.Close();
        }
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = @"/C powershell -ExecutionPolicy Bypass -WindowStyle Hidden -command c:'Temp'Email2Send.ps1";
        process.StartInfo = startInfo;
        process.Start();

编辑:没关系,我在发帖后2秒找到了解决方案。由于某些原因,powershell在Windows路径中的某些计算机上没有正确链接。将powershell更改为:%SYSTEMROOT%'System32'WindowsPowerShell'v1.0'powershell.exe

通过powershell发送电子邮件不工作

我在发布后2秒找到了解决方案。由于某些原因,powershell在Windows路径中的某些计算机上没有正确链接。将powershell更改为:%SYSTEMROOT%'System32'WindowsPowerShell'v1.0'powershell.exe