powershell - 在PowerShell上的Send-MailMessage不起作用

标签 powershell email

我有一个大问题。对于一个学校俱乐部,我必须编写一个自动发送电子邮件的脚本。我决定为此使用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/

相关文章:

powershell - Powershell运行历史记录删除

.net - 通过 NuGet 提供命令行工具以在 Visual Studio 中的生成后事件中使用

php - 分隔多封电子邮件的最佳分隔符是什么?

image - 无法使用 PowerShell 捕获的镜像创建新的 AzureVM

jquery - Powershell:通过 InternetExplorer.Application 使用 jQuery

powershell - Solana 钱包验证失败?

node.js - 邮戳:发送带有模板的电子邮件

java - SendEmail 在 Selenium Webdriver 中无法工作 - 未知 SMTP 主机 : smtp. gmail.com;

linux - 防止 Mutt 发送到默认电子邮件地址

email - 通过进程处理 "send email"步骤的可重用且稳健的模式