views:

2840

answers:

5

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
+5  A: 

I'm not sure you can change port numbers with Send-MailMessage since gmail works on port 587. Anyway, here's how to send email through gmail with .NET SmtpClient:

$smtpClient = new-object system.net.mail.smtpClient 
$smtpClient.Host = 'smtp.gmail.com'
$smtpClient.Port = 587
$smtpClient.EnableSsl = $true
$smtpClient.Credentials = [Net.NetworkCredential](Get-Credential GmailUserID) 
$smtpClient.Send('[email protected]','[email protected]','test subject', 'test message')
Shay Levy
A: 

I haven't used PowerShell V2 send-mailmessage, but I have used System.Net.Mail.SMTPClient class in V1 to send messages to a gmail account for demo purposes. This might be overkill but run an smtp server on my Vista laptop see this link, if you're in an enterprise you will already have a mail rely server and this step isn't necessary. Having an smtp server I'm able to send email to my gmail account with the following code:

$smtpmail = [System.Net.Mail.SMTPClient]("127.0.0.1")
$smtpmail.Send("[email protected]", "[email protected]", "Test Message", "Message via local smtp")
Chad Miller
+3  A: 

Just found this question .. here's my PowerShell Send-MailMessage Sample for Gmail.. Tested and working solution:

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

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

Christian
thanks I'm a serverfault/superuser member and this helped me make a script to see when my server reboots.
jer.salamon
A: 

Christian,

I used your solution for GMail - thanks! - but I have a question: how do I add an attachment there? I'm very new to PS and scripting in general, can't figure it out :-/

asmodeus
+1  A: 

I used Christian's Feb 12 solution and I'm also just beginning to learn PowerShell. As far as attachments, I was poking around with Get-Member learning how it works and noticed that Send() has two definitions... the second definition takes a System.Net.Mail.MailMessage object which allows for Attachments and many more powerful and useful features like Cc and Bcc. Here's an example that has attachments (to be mixed with his above example):

# append to Christian's code above --^
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = $EmailFrom
$emailMessage.To.Add($EmailTo)
$emailMessage.Subject = $Subject
$emailMessage.Body = $Body
$emailMessage.Attachments.Add("C:\Test.txt")
$SMTPClient.Send($emailMessage)

Enjoy!

core