Sending mail from Powershell via Gmail SMTP

Viewed 2085

I am trying to create a powershell script that should send a mail using Gmail SMTP service. This is my powershell version -

Get-Host | Select-Object Version

Version
-------
5.1.14393.3866

After researching & trying different code, I am unable to send Email from Powershell via Gmail SMTP. Here is my code:

$emailSmtpServer = "smtp.gmail.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "mymail@gmail.com"
$emailSmtpPass = "mymailpass"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "mymail@gmail.com"
$emailMessage.To.Add("myothermail@mail.com")
$emailMessage.Subject = "Small mail for a friend"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = @"
<p><strong>Hello me</strong>.</p>
<p>It seems to work</p>
<p>JP</p>
"@

$SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );

$SMTPClient.Send( $emailMessage )

Whenever I am running this code powershell is throwing below error:

Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not
authenticated. The server response was: 5.7.0 Authentication Required. Learn more at"
At C:\Users\ritu\Documents\testing.ps1:38 char:1
+ $SMTPClient.Send( $emailMessage )
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

I have already enable "Authenticate with Less Secured App" in my Gmail settings.

Need some help to fix this. Also if anyone can suggest how to add attachment, that will be very helpful.

UPDATE & FIX:

As I understood from Google Support, currently for personal Gmail smtp, we need to setup 2 factors authentication. But when we use gsuite account, everything went fine.

3 Answers

Less secure apps no longer supported by google after 30 May 2022 .To solve Powershell gmail error message '5.7.0 Authentication Required' , you need turn on App password for your google account using below steps.

  1. Login to your Gmail , http://gmail.google.com
  2. After login, go to https://myaccount.google.com/signinoptions/two-step-verification , to make sure you enable "two step verification" on your gamil account.
  3. Then Create your application password via https://security.google.com/settings/security/apppasswords
  4. Below screen will loaded , click "Select app" drop-down control and select "Other" , then give a value to it , eg PowershellApp , then click "GENERATE" button enter image description here
  5. You will get a 16 digits App password , now note it down.
  6. Finally , go https://accounts.google.com/DisplayUnlockCaptcha enable "external access to your Google account"

Now you shall able send gmail via Powershell , using code like below

$userName = 'MyGamial@gmail.com'
$To = 'receipient@email.com'
# The 16 digits gmail Acc app password
$password = 'aaaabbbbccccdddd'    
[SecureString]$securepassword = $password | ConvertTo-SecureString -AsPlainText -Force 
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securepassword
Send-MailMessage -SmtpServer smtp.gmail.com -Port 587 -UseSsl -From $userName -To $To -Subject 'Test subject' -Body 'Test message' -Credential $credential

Both the scripts given above would work, The actual issue is with google trying to block as mentioned in the Update on the first post. If you are using personal gmail account, you would need to create a App Password https://myaccount.google.com/apppasswords even after setting with Two factor authentication. Just replace the new password generated in any of the "password" fields and the mail will be triggered.

If you already enabled https://myaccount.google.com/lesssecureapps
Simply "Send-MailMessage" works :

$cred = Get-Credential # mymail@gmail.com / mymailpass
Send-MailMessage -UseSsl -SmtpServer "smtp.gmail.com" -Credential $cred -Subject "Sujet" -Body "Le corps" -To "myothermail@mail.com" -From "mymail@gmail.com"

Tested with Powershell 5.1.19041.610 / Windows 10

Related