以下是我正在使用的Powershell脚本代码
$SmtpClient = New-Object system.net.mail.smtpClient;
$mailmessage = New-Object system.net.mail.mailmessage;
$SmtpClient.Host = "exchange server name";
$SmtpClient.Credentials = New-Object System.Net.NetworkCredential("User name", "password");
$mailmessage.from = ("user@domain.com");
$mailmessage.To.add("user@domain.com");
$mailmessage.Subject = “Message”;
$mailmessage.Body = “Body”;
$smtpclient.Send($mailmessage);
这是我得到的错误
使用“1”参数调用“发送”的异常:“发送邮件失败”。 在C:\ Users \ user1 \ Desktop \ sendmailPS.ps1:9 char:17 + $ smtpclient.Send<<<< (MAILMESSAGE $); + CategoryInfo:未指定:(:) [],MethodInvocationExcept ion + FullyQualifiedErrorId:DotNetMethodException
任何帮助都表示赞赏,并提前致谢
答案 0 :(得分:2)
除非您需要保持与PowerShell 1.0的兼容性,否则请改用send-mailmessage
。添加了换行符以提高可读性。
send-mailmessage -smtphost your.smtp.host -from user@domain.com -to user@domain.com `
-subject "message" -body "body" `
-Credential (New-Object -TypeName System.Management.Automation.PSCredential`
-ArgumentList "user name" (ConvertTo-SecureString -String "password" -AsPlainText -Force))