我有以下脚本:
$From = Read-Host "Enter your gmail address"
$UserName = Read-Host "Enter your gmail username (address without @gmail.com)"
$UserPass = Read-Host "Enter your Gmail Password (will not be stored)" -AsSecureString
$SecurePassword = $UserPass| ConvertTo-SecureString -AsPlainText -Force;
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword;
$To = Read-Host "Who are you sending this to?"
$Subject = Read-Host "Subject"
$Body = Read-Host "Message"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl -Credential $Credential
它正在响应此错误(请注意,如果这很重要,这不是管理员帐户):
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was:
5.5.1 Authentication Required. Learn more at
At C:\Users\Server\SERVER\Appdata\sendemail.ps1:11 char:1
+ Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -S ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
编辑:我最初的问题中有一个拼写错误(我把$Cedential
改为$Credential
)。这个问题现在已经修复,但还有一个错误我已改正,因为常见的拼写错误没什么用。
答案1
$EmailFrom = "[电子邮件保护]“
$EmailTo = "[电子邮件保护]“
$Subject =“来自 XYZ 的通知”
$Body =“这是来自 XYZ Notifications 的通知..”
$SMTP服务器 = “smtp.gmail.com”
$SMTPClient = 新对象 Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("用户名", "密码");
$SMTP客户端.发送($电子邮件发件人,$电子邮件收件人,$主题,$正文)
通过 https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage