我想測試發送電子郵件,但我不想在腳本中只有明文密碼。使用安全密碼通過Powershell發送電子郵件
這裏是我有,這一點也適用:
$SmtpServer = 'smtp.office365.com'
$SmtpUser = '[email protected]'
$smtpPassword = 'Hunter2'
$MailtTo = '[email protected]'
$MailFrom = '[email protected]'
$MailSubject = "Test using $SmtpServer"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials
這工作。
我遵循this stackoverflow thread的建議,因爲我希望此腳本在沒有提示憑據(或輸入明文)的情況下運行,因此我可以將其作爲計劃任務運行。
我有我已經運行時創建一個安全密碼:
read-host -assecurestring | convertfrom-securestring | out-file C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt
但如果我代替我,用建議的條目$ smtpPassword項:
$SmtpServer = 'smtp.office365.com'
$SmtpUser = '[email protected]'
$smtpPassword = cat C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt | convertto-securestring
$MailtTo = '[email protected]'
$MailFrom = '[email protected]'
$MailSubject = "Test using $SmtpServer"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $($smtpPassword | ConvertTo-SecureString -AsPlainText -Force)
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials
然後電子郵件不發送了。我收到以下錯誤:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
任何提示?我想將此電子郵件腳本作爲計劃任務運行,但我不希望以明文形式保存密碼。