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

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

问题描述

我试图弄清楚如何使用PowerShell V2的 Send-MailMessage 与gmail。



到目前为止。

  $ ss = new-object Security.SecureString 
foreach($ ch inpassword .ToCharArray())
{
$ ss.AppendChar($ ch)
}
$ cred = new-object Management.Automation.PSCredentialuid@domain.com ,$ ss
Send-MailMessage -SmtpServer smtp.gmail.com -UseSsl -Credential $ cred -Body ...

我收到以下错误:

  Send-MailMessage:SMTP服务器需要安全连接或客户端未通过验证。服务器响应是:5.5.1需要身份验证。在
处了解更多
在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上)?

一些额外的限制


  1. 我希望这是非交互式的,所以 get-credential 不会工作

  2. 用户帐户不在gmail上域名,但谷歌应用程序注册域名

  3. 对于这个问题,我只对 Send-MailMessage cmdlet感兴趣,通过正常的.Net API很好理解。
  4. 我的PowerShell Send-MailMessage示例适用于Gmail。经过测试的工作解决方案:

      $ EmailFrom =notificat ions@somedomain.com
    $ EmailTo =me@earth.com
    $ Subject =来自XYZ的通知
    $ Body =这是来自XYZ通知的通知..
    $ SMTPServer =smtp.gmail.com
    $ SMTPClient =新对象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 ..



    也许这对其他人遇到这个问题..


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

    Here's what I have so far.

    $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...
    

    I get the following error

    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
    

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

    Some additional restrictions

    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 an 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.

    解决方案

    Just found this question .. 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)
    

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

    maybe this is a help for other coming across this question ..

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

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