在PowerShell上的Send-MailMessage不起作用 [英] Send-MailMessage on PowerShell does not work
问题描述
我有一个大问题。对于一个学校俱乐部,我必须编写一个自动发送电子邮件的脚本。我决定为此使用PowerShell。
I have a big problem. For a school club I have to write a script which sends emails automatically. I decided to use PowerShell for that.
我的代码:
Send-MailMessage –To "email@outlook.com" –Subject "Test E-Mail" –Body "FIRST EMAIL WITH POWERSHELL" –SmtpServer "smtp.gmail.com" –From "email@gmail.com"
我的错误代码:
Send-MailMessage:SMTP服务器需要安全连接或客户端尚未通过身份验证。服务器响应为:
5.7.0必须首先发出STARTTLS命令。 o3sm51888528wrs.30-gsmtp
行:1个字符:1
+ Send-MailMessage-至 email@outlook.com主题 Test E-Mail -Bod ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~
+ CategoryInfo:InvalidOperation:(System.Net.Mail.SmtpClient:SmtpClient)[Send-MailMessage],SmtpException
+ FullyQualifiedErrorId:SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
Send-MailMessage: The SMTP server requires a secure connection or the client has not been authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. o3sm51888528wrs.30 - gsmtp In line: 1 character: 1 + Send-MailMessage -To "email@outlook.com" Subject "Test E-Mail" -Bod ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidOperation: (System.Net.Mail.SmtpClient: SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId: SmtpException, Microsoft.PowerShell.Commands.SendMailMessage
如果我写
–SmtpServer "gsmtp.gmail.com"
错误代码为:
Send-MailMessage:无法建立到远程服务器的连接。
第一行:1个字符:1
+ Send-MailMessage-至 email@outlook.com;-主题 Test E-Mail; -董事会。 。
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:InvalidOperation:(系统.Net。邮件.SmtpClient:SmtpClient)[Send-MailMessage], SmtpException
+ FullyQualifiedErrorId:SmtpException,微软电源外壳。命令。 SendMailMessage
Send-MailMessage : The connection to the remote server cannot be established. In line:1 character:1 + Send-MailMessage -To "email@outlook.com"; -Subject "Test E-Mail"; -Bod . . . + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System. Net. Mail. SmtpClient:SmtpClient) [Send-MailMessage], SmtpException + FullyQualifiedErrorId : SmtpException,Microsoft. PowerShell. Commands. SendMailMessage
您知道出了什么问题吗?
Do you know what´s the problem?
推荐答案
首先,您需要在Google帐户设置中启用安全性较低的应用。
为此,请点击以下链接,然后设置安全应用设置为打开。
First, you will need to enable "less secure apps" in your Google account settings. To do so, click on the following link, then set the Allow less secure apps setting to ON.
完成后,您可以使用发送消息-MailMessage
。这是一个工作示例。
Once this is done, you can send your message with Send-MailMessage
. Here's a working example.
$MyEmail = "email@gmail.com"
$creds = get-credential -Message 'Enter your email credentials' -Username $MyEmail
$ServerParams = @{
SmtpServer = "smtp.gmail.com"
From = $MyEmail
Port = 587
UseSsl = $true
Credential = $creds
}
$MessageParams = @{
'To' = "email@outlook.com"
'Subject' = "Test E-Mail"
'Body' = "FIRST EMAIL WITH POWERSHELL"
}
Send-MailMessage @ServerParams @MessageParams
奖金:为了获得更好的可读性和可重用性,您可以使用参数
Bonus: For a better readability and reusability, you can splat your parameters
(参考:关于Splatting )
这篇关于在PowerShell上的Send-MailMessage不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!