【发布时间】:2020-03-19 20:07:00
【问题描述】:
我正在尝试在发送电子邮件的脚本中使用存储的加密密码,但我不断收到错误消息:
send-mailmessage : SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证发送匿名邮件 [DM6PR66666019.namprd456.prod.outlook.com]
我使用以下代码创建文本文件:
$passwordtostore = 'NotTheRealPassword$9gv8z6VHnPfDd8zc'
$secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
$secureStringText = $secureStringPWD | ConvertFrom-SecureString
Set-Content "c:\temp\scriptsencrypted_password1.txt" $secureStringText
我使用以下导入密码:
$passwordFile = "c:\temp\scriptsencrypted_password1.txt"
$password = Get-Content $passwordFile | ConvertTo-SecureString
这是我正在使用的 sendmail 功能:
function SendMail($ToEmails,$FromEmail,$Subj,$Body,$UserName,$Password){
$cred = New-Object System.Management.Automation.PSCredential $UserName,$Password
$MailParams=@{"from"=$FromEmail; "to"=$ToEmails;"body"=$Body;"subject"=$Subj;"smtpserver"="smtp.office365.com"}
send-mailmessage @MailParams -Credential $cred -UseSsl $true -port 587
}
这是调用该函数的代码:
$alertEmail = "me.stillme@mydomain.com"
$username="psemail@mydomain.com"
$passwordFile = "c:\temp\scriptsencrypted_password1.txt"
$password = Get-Content $passwordFile | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $password)
Import-Module -Name "..\SendMail.psm1"
... Doing some stuff
SendMail $alertEmail $username "This is the subject" "this is the body" $credential.UserName $credential.Password
【问题讨论】:
标签: powershell sendmail