首页 > 解决方案 > 无法使用 PowerShell 脚本发送电子邮件

问题描述

我无法使用以下脚本发送电子邮件

$fromemail = "pap@domainname.com"
$users     = "pap@domainname.com"
$Server    = "smptserver.domainname.com"

Send-MailMessage -From $fromemail -To $users -Subject "Disk Space Monitoring Report" -Body "Hello world" -Priority High -SmtpServer $server

这是输出屏幕下方显示的错误消息:

Send-MailMessage :无法解析远程名称:
'smptserver.domainname.com'
在行:10 字符:2
+ Send-MailMessage -From $fromemail -To $users -Subject "Disk Space Mo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

标签: powershellpowershell-2.0powershell-3.0email

解决方案


认为您需要将变量写为字符串还使用 smtp 服务器的 ip 地址而不是 dns 名称

> [string]$stmp = 0.0.0.0 

 [string]$emailfrom = email@company.com

> [string]$emailtoo = email@company.com (if you add more than one email

> address here add a [] at the end of string like so [string[]]$emailtoo)

推荐阅读