我正在尝试弄清楚如何使用 PowerShell V2Send-MailMessage
with Gmail.
这是我到目前为止所拥有的。
$ss = New-Object Security.SecureString
foreach ($ch in "password".ToCharArray())
{
$ss.AppendChar($ch)
}
$cred = New-Object Management.Automation.PSCredential "[email protected]", $ss
Send-MailMessage -SmtpServer smtp.gmail.com -UseSsl -Credential $cred -Body...
我收到以下错误
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 foo.ps1:18 char:21
+ Send-MailMessage <<<< `
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
是我做错了什么,还是Send-MailMessage
还没有完全烤好(我正在CTP 3)?
一些额外的限制:
- 我希望这是非交互式的,所以Get-Credential行不通的。
- 用户帐户不在 Gmail 域中,而是在谷歌应用套件注册域名。
- 对于这个问题,我只对
Send-MailMessage
cmdlet。通过普通的 .NET API 发送邮件是很好理解的。
这是我的 PowerShell发送邮件消息Gmail 示例...
经过测试和工作的解决方案:
$EmailFrom = "[email protected]"
$EmailTo = "[email protected]"
$Subject = "Notification from XYZ"
$Body = "this is a notification from XYZ Notifications.."
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
只需更改 $EmailTo 和 $SMTPClient.Credentials 中的用户名/密码...请勿在用户名中包含 @gmail.com...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)