Powershell 发送邮件失败

Powershell send email failure

我正在编写 Powershell 电子邮件功能,我使用了 comdle

"Send-MailMessage -To mail -From mail -Subject" 

我遇到错误:

无法连接到远程服务器。

另外,请问这个方法行得通吗?

New-Object System.Net.Mail.MailMessage($From, $To, $Subject, $Body)

与 Send-MailMessage 有什么区别?

首先,您需要检查您的 SMTP,看看您是否可以解析或 ping 您的 SMTP 服务器。另外,您是否检查过您的端口,例如587?像这样:

Send-MailMessage -To $to -From $from -cc $cc -Subject $sub -Body $body -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587

发送邮件的区别,也可以参考以下link:

Scripting: Send email using Powershell and System.Net.Mail API