我试图弄清楚如何将PowerShell V2的Send-MailMessageGmail一起使用。

到目前为止,这就是我所拥有的。

$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域上,而是在Google Apps注册的域上。
  • 对于这个问题,我只对Send-MailMessage cmdlet感兴趣。通过普通的.NET API发送邮件已广为人知。
  • 最佳答案

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

    经过测试的工作解决方案:

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

    关于security - 使用PowerShell V2的Send-MailMessage通过Gmail发送邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1252335/

    10-13 07:50