大家在使用send-mailmessge 编写一些脚本的时候一般会使用匿名发送的方式,如下所示:
send-mailmessage -to "test@contoso.com" -from "test@contoso.com" -subject "mailtest" -SmtpServer mail.contoso.com
但是在smtpserver禁止匿名发送的情况下,应该怎么办呢?好多人说可以调用-credential参数。
但是在控制台默认输入-credential一般会弹出窗口提示输入密码,没办法调用到脚本里。有没有一种法可以将密码进行处理呢?
举例说明:
$password = convertto-securestring "password.2013" -asplaintext -force
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist "test",$password
send-mailmessage -to "test@contoso.com" -from "test@contoso.com" -subject "mailtest" -credential $cred -SmtpServer mail.contoso.com
$cred=new-object -typename System.Management.Automation.PSCredential -argumentlist "test",$password
send-mailmessage -to "test@contoso.com" -from "test@contoso.com" -subject "mailtest" -credential $cred -SmtpServer mail.contoso.com
这样可以很完美的调用一个使用SMTP验证的邮件发送脚本。
本文转自handsome7038 51CTO博客,原文链接:http://blog.51cto.com/lixiaosong/1322643