Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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 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.
like image 674
Scott Weinstein Avatar asked Aug 09 '09 21:08

Scott Weinstein


People also ask

How do I send email from PowerShell to Gmail?

Use the Powershell command-line to send an email using Gmail. Enter the Gmail account credentials. If the test was successful, you will receive the sent message. Send the email with an attachment using Powershell.

How does PowerShell send-MailMessage work?

The Send-MailMessage cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The Cc parameter sends a copy of the message to the specified recipient. The Bcc parameter sends a blind copy of the message.

What can I use instead of MailMessage?

NET MailKit. The most generic method that would replace SmtpClient and Send-MailMessage would be the recommended replacement, which is MailKit. This is a third-party, open-source library but maintained by a Microsoft employee and officially recommended for use in the documentation.

How do you send an automatic email in PowerShell?

To send email messages through an SMTP server, you can use the Send-MailMessage PowerShell cmdlet. You can use this built-in cmdlet to send emails in PowerShell version 2.0 and newer (previously you can use the . Net System. Net.Mail class to send emails).


1 Answers

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

like image 116
Christian Casutt Avatar answered Sep 20 '22 23:09

Christian Casutt