3

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.

  • Does this answer your question? [Send mail via Gmail with PowerShell V2's Send-MailMessage](https://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage) – tripleee Feb 04 '21 at 11:41
  • @tripleee I checked this, but that also giving me same problem. Not sure whether something else in blocking or what is the problem. – Rituparno Bhattacharya Feb 04 '21 at 12:10
  • I _think_ the problem could be that "less secure app" is no longer permitted, and you need a dedicated app password; but fortunately, I don't have a Windows system where I could explore this. – tripleee Feb 04 '21 at 12:12
  • @tripleee Possibly you are right. I found this here - https://stackoverflow.com/questions/32515245/how-to-to-send-mail-using-gmail-in-laravel .... Let me try this out. – Rituparno Bhattacharya Feb 04 '21 at 12:19
  • Does [this answer](https://stackoverflow.com/a/39967829/9898643) help? – Theo Feb 04 '21 at 13:09
  • @Theo I tried this also, but same error. Believe tripleee is right. I need to create App Password to use Gmail SMTP. Seems Less Secure App is no longer permitted by Google. – Rituparno Bhattacharya Feb 05 '21 at 05:28

3 Answers3

4

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
HO LI Pin
  • 1,441
  • 13
  • 13
2

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.

Amit Joshi
  • 36
  • 3
1

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

PlageMan
  • 708
  • 7
  • 21