我有一个大问题。对于一个学校俱乐部,我必须编写一个自动发送电子邮件的脚本。我决定为此使用PowerShell。
我的代码:
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: 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 : 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
你知道是什么问题吗?
最佳答案
首先,您需要在Google帐户设置中启用“安全性较低的应用”。
为此,请单击following link,然后将允许不安全的应用程序设置设置为 ON 。
完成此操作后,您可以使用Send-MailMessage
发送消息。这是一个工作示例。
$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
奖励:为了获得更好的可读性和可重用性,您可以设置参数
(引用:About Splatting)
关于powershell - 在PowerShell上的Send-MailMessage不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54134963/