使用安全密码通过Powershell发送电子邮件 [英] Sending email via Powershell using secure password

查看:166
本文介绍了使用安全密码通过Powershell发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试测试发送电子邮件,但是我不想只在脚本中输入明文密码。

I'm trying to test sending an email but I don't want to just have a plaintext password in my script.

这就是我所拥有的,作品:

Here's what I have, and this works:

$SmtpServer = 'smtp.office365.com'
$SmtpUser = 'fubsygamr@office365.com'
$smtpPassword = 'Hunter2'
$MailtTo = 'fubsygamr@office365.com'
$MailFrom = 'fubsygamr@office365.com'
$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 

我遵循了此stackoverflow线程,因为我希望此脚本可以运行提示输入凭据(或输入纯文本),以便将其作为计划任务运行。

I followed the advice from this stackoverflow thread, as I'd like this script to run without prompting for credentials (or entering them plaintext), so I can run it as a scheduled task.

我有一个通过运行创建的安全密码:

I have a secure password I've created by running:

read-host -assecurestring | convertfrom-securestring | out-file C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt

但是我将$ smtpPassword条目替换为建议的条目:

But if I replace my $smtpPassword entry with the suggested entry:

$SmtpServer = 'smtp.office365.com'
$SmtpUser = 'fubsygamr@office365.com'
$smtpPassword = cat C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt | convertto-securestring
$MailtTo = 'fubsygamr@office365.com'
$MailFrom = 'fubsygamr@office365.com'
$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 

然后,电子邮件不再发送。我收到以下错误:

Then the email doesn't send anymore. I get the following error:


Send-MailMessage:SMTP服务器需要安全连接或客户端未通过身份验证。服务器响应为:5.7.57 SMTP;客户端未通过身份验证以在MAIL FROM

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

发送邮件期间发送
匿名邮件任何提示?我想将此电子邮件脚本作为预定任务运行,但是我不希望密码以明文形式保存。

Any tips? I'd like to run this email script as a scheduled task, but I don't want my password saved in plaintext.

推荐答案

同事帮助我意识到 $ Credentials 对象试图将密码转换回纯文本。我删除了 ConvertTo-SecureSTring -AsPlainText -Force 修饰符,并成功发送了电子邮件!

Coworker helped me realize the $Credentials object was trying to convert my password back to plaintext. I removed the ConvertTo-SecureSTring -AsPlainText -Force modifier, and the email sent successfully!

功能脚本:

$SmtpServer = 'smtp.office365.com'
$SmtpUser = 'fubsygamr@office365.com'
$smtpPassword = cat C:\Users\FubsyGamr\Documents\mysecurestring_fubsygamr.txt | convertto-securestring
$MailtTo = 'fubsygamr@office365.com'
$MailFrom = 'fubsygamr@office365.com'
$MailSubject = "Test using $SmtpServer"
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $SmtpUser, $smtpPassword
Send-MailMessage -To "$MailtTo" -from "$MailFrom" -Subject $MailSubject -SmtpServer $SmtpServer -UseSsl -Credential $Credentials 

这篇关于使用安全密码通过Powershell发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆