使用 PowerShell V2 的 Send-MailMessage 通过 Gmail 发送邮件 [英] Send mail via Gmail with PowerShell V2's Send-MailMessage

查看:26
本文介绍了使用 PowerShell V2 的 Send-MailMessage 通过 Gmail 发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何使用 PowerShell V2 的 Send-MailMessageGmail.

I'm trying to figure out how to use PowerShell V2's Send-MailMessage with Gmail.

这是我目前所拥有的.

$ss = New-Object Security.SecureString
foreach ($ch in "password".ToCharArray())
{
    $ss.AppendChar($ch)
}
$cred = New-Object Management.Automation.PSCredential "uid@domain.com", $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)?

Am I doing something wrong, or is Send-MailMessage not fully baked yet (I'm on CTP 3)?

一些额外的限制:

  1. 我希望这是非交互式的,所以 Get-Credential 不起作用.
  2. 用户帐户不在 Gmail 域中,而是在 Google Apps 注册域中.
  3. 对于这个问题,我只对 Send-MailMessage cmdlet 感兴趣.通过普通的 .NET API 发送邮件很容易理解.
  1. I want this to be non-interactive, so Get-Credential won't work.
  2. The user account isn't on the Gmail domain, but a Google Apps registered domain.
  3. For this question, I'm only interested in the Send-MailMessage cmdlet. Sending mail via the normal .NET API is well understood.

推荐答案

这是我的 PowerShell Send-MailMessage Gmail 示例...

Here's my PowerShell Send-MailMessage sample for Gmail...

经过测试且有效的解决方案:

Tested and working solution:

$EmailFrom = "notifications@somedomain.com"
$EmailTo = "me@earth.com"
$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)

只需在 $SMTPClient.Credentials 中更改 $EmailTo 和用户名/密码...不要在您的用户名中包含 @gmail.com...

Just change $EmailTo, and username/password in $SMTPClient.Credentials... Do not include @gmail.com in your username...

这篇关于使用 PowerShell V2 的 Send-MailMessage 通过 Gmail 发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆