Send mail via gmail with PowerShell V2's Send-MailMessage

Posted by Scott Weinstein on Stack Overflow See other posts from Stack Overflow or by Scott Weinstein
Published on 2009-08-09T21:28:02Z Indexed on 2010/06/02 4:13 UTC
Read the original article Hit count: 1172

Filed under:
|
|

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 "[email protected]", $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)?

Edit - two 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

© Stack Overflow or respective owner

Related posts about security

Related posts about powershell