0

I am trying to send an email from powershell. This is my powershell script:

$EmailParams = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential  = 'email id here'
From = 'emailidhere@gmail.com'
To = 'emailidhere@gmail.com'
Subject = 'Sending emails from Gmail with PowerShell'
Body = "Email is just Wonderful!"
}
$EmailParams
Send-MailMessage @EmailParams

I am facing the following error: enter image description here

Captain Levi
  • 55
  • 1
  • 8
  • I have disabled 2 step verification for google still I am getting the same error. @GuentherSchmitz – Captain Levi Aug 06 '21 at 08:19
  • Code is fine, indeed as Guenther says looks like the same thread. You will have to adjust security settings! https://www.google.com/settings/security set access for less secure apps there. – Bernard Moeskops Aug 06 '21 at 08:20
  • I used to use a Gmail account a lot for emailing via powershell, but I kept on getting these issues. It seems to reset the lowered security settings and kept on giving warnings and now I only use other email providers like O365. – Bernard Moeskops Aug 06 '21 at 08:22
  • @BernardMoeskops these security issues are there in outlook or not? If not, I will switch to outlook too as I don't feel comfortable lowering the security settings in google. – Captain Levi Aug 06 '21 at 08:32
  • I don't have these issues in Outlook, not with exchange on-prem nor with O365. That is why I stopped using Gmail for this. If you want to use Gmail I believe you need to setup a better authentication and create an app with token etc. (not 100% sure). – Bernard Moeskops Aug 06 '21 at 09:51
  • 1
    Ok I think I will try Outlook. Thank you @BernardMoeskops – Captain Levi Aug 06 '21 at 11:18
  • 2
    like @BernardMoeskops mentions it seems you need to create a token for sending mails from gmail today. this tutorial might help: https://techexpert.tips/powershell/powershell-send-email-gmail/ – Guenther Schmitz Aug 07 '21 at 06:50

0 Answers0