使用加密密码发送邮件失败
Using encrypted password to send mail is failing
我正在尝试在发送电子邮件的脚本中使用存储的加密密码,但我不断收到错误消息:
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
[DM6PR66666019.namprd456.prod.outlook.com]
我使用以下代码创建了文本文件:
$passwordtostore = 'NotTheRealPasswordgv8z6VHnPfDd8zc'
$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
这是我正在使用的发送邮件功能:
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
就我个人而言,我不明白为什么要在需要为 Send-MailMessage cmdlet 导入的模块中创建一个函数。
这使得事情变得更难使用。
此外,您似乎在函数内部切换电子邮件地址。
无论如何,创建凭据时出现问题,将其拆分为用户名和(安全)密码以作为参数发送给函数,然后将它们重新组合到其中的凭据对象中。
为什么不跳过那个模块函数而简单地做:
$password = Get-Content 'c:\temp\scriptsencrypted_password1.txt' -Raw | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PsCredential('YourLoginName', $password)
$MailParams=@{
From = 'me.stillme@mydomain.com'
To = 'psemail@mydomain.com'
Body = "this is the body"
Subject = "This is the subject"
SmtpServer = 'smtp.office365.com'
Port = 587
UseSsl = $true
Credential = $credential
}
Send-MailMessage @MailParams
这将使您的脚本更加readable/maintainable
我在上面的代码中发现了两个问题:
- 密码中未转义的 $。
- 一旦我将“-NoNewline”添加到设置内容中,它就开始工作了。
因此,要创建加密文件:
$passwordtostore = "7K9CBgvc4rttvfctrsef6PVHqnP6fDdwhatevervtfdscttzSc"
$secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
$secureStringText = $secureStringPWD | ConvertFrom-SecureString
Set-Content "D:\Powershell Scripts\encrypted.hash" $secureStringText -NoNewline
然后检索密码并在凭据中使用它:
$password = Get-Content $passwordFile -Raw | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $password)
这不是问题的根源,但我确实执行了@Theo 的建议:使用 send-mailmessage。
我正在尝试在发送电子邮件的脚本中使用存储的加密密码,但我不断收到错误消息:
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 [DM6PR66666019.namprd456.prod.outlook.com]
我使用以下代码创建了文本文件:
$passwordtostore = 'NotTheRealPasswordgv8z6VHnPfDd8zc'
$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
这是我正在使用的发送邮件功能:
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
就我个人而言,我不明白为什么要在需要为 Send-MailMessage cmdlet 导入的模块中创建一个函数。 这使得事情变得更难使用。
此外,您似乎在函数内部切换电子邮件地址。
无论如何,创建凭据时出现问题,将其拆分为用户名和(安全)密码以作为参数发送给函数,然后将它们重新组合到其中的凭据对象中。
为什么不跳过那个模块函数而简单地做:
$password = Get-Content 'c:\temp\scriptsencrypted_password1.txt' -Raw | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PsCredential('YourLoginName', $password)
$MailParams=@{
From = 'me.stillme@mydomain.com'
To = 'psemail@mydomain.com'
Body = "this is the body"
Subject = "This is the subject"
SmtpServer = 'smtp.office365.com'
Port = 587
UseSsl = $true
Credential = $credential
}
Send-MailMessage @MailParams
这将使您的脚本更加readable/maintainable
我在上面的代码中发现了两个问题:
- 密码中未转义的 $。
- 一旦我将“-NoNewline”添加到设置内容中,它就开始工作了。
因此,要创建加密文件:
$passwordtostore = "7K9CBgvc4rttvfctrsef6PVHqnP6fDdwhatevervtfdscttzSc"
$secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
$secureStringText = $secureStringPWD | ConvertFrom-SecureString
Set-Content "D:\Powershell Scripts\encrypted.hash" $secureStringText -NoNewline
然后检索密码并在凭据中使用它:
$password = Get-Content $passwordFile -Raw | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $password)
这不是问题的根源,但我确实执行了@Theo 的建议:使用 send-mailmessage。