Send-MailMessage在POWERSHELL 7.0以上版本中报obsolete 2021-10-14 Website News 暂无评论 3519 次阅读 Windows 2012版本中自带的PowerShell版本是3.0的,当在3.0下能正常使用的Send-MailMessage函数,在PowerShhell v7.0以上版本就会出现一下错误: Currently Send-MailMessage is marked obsolete, but I don't think it should be. The API behind it should be replaced eventually, but the site that gives the reasons why it should be used, also include a reason what it would still be useful for. 网上找了一个解决方法,可以用.Net的对象objects,来解决Send-MailMessage过时的问题。 ```csharp $userName = "user@domain.com" $password = ConvertTo-SecureString –String "SMTPASSWORD" –AsPlainText -Force $credentials = New-Object System.Management.Automation.PsCredential($userName, $password) # SMTP Variables $emailFrom = "info@domain.com" $emailTo = "user@remote-domain.com" $subject = "Test" $body = "Some test message." $SMTPServer = "smtp.domain.com" $SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = $credentials $SMTPClient.Send($emailFrom, $emailTo, $subject, $body) ``` 标签: .NET, PowerShell, Send-MailMessage, obsolete, SmtpClient, OBJECTS 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭