PowerShell SMTPClient在Windows 7上失败(“需要身份验证”);适用于Windows 10

问题描述 投票:0回答:1

以下PowerShell脚本片段在带有最新.NET(4.8)的Windows 10(PSv5.1)上正常运行。

在Windows 7(PSV2.0)上,服务器响应失败:“需要授权”。但是,如果我将$ sendercreds.Password替换为实际的明文密码,则可以使用!

$sendercreds = Get-Credential
$smtp = New-Object System.Net.Mail.SmtpClient
$to = New-Object System.Net.Mail.MailAddress("[email protected]")
$from = New-Object System.Net.Mail.MailAddress("[email protected]")
$msg = New-Object System.Net.Mail.MailMessage($from, $to)
$msg.subject = "Something I want to tell them"
$smtp.Host = "smtpserver.senderhost.com"
$smtp.EnableSsl = $True
$smtp.Port = 587
$smtp.Credentials = New-Object System.Net.NetworkCredential($sendercreds.UserName, $sendercreds.Password)
$smtp.Send($msg)

我将Win7机器.NET从4.6.1更新为4.8:不高兴...

[似乎与PS2.0中对SecureString的支持有关,但是经过数小时的敲击,我在网上找不到任何有关此的信息!

powershell smtpclient securestring
1个回答
0
投票

经过更多的搜索,我终于找到了:https://www.pdq.com/blog/powershell-running-net-4-with-powershell-v2/。事实证明PSV2.0不会使用您已经安装的最新.NET Framework,并通过.config文件告诉它!

配置(XML)文件(powershell.exe.config和powershell_ise.exe.config)需要包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0.30319" />
      <supportedRuntime version="v2.0.50727" />
    </startup>
</configuration>
© www.soinside.com 2019 - 2024. All rights reserved.