send gmail using powershell [duplicate]

2019-07-23 20:12发布

问题:

This question already has an answer here:

  • sending email with gmail using powershell 3 answers

I'm developing a simply monitoring too for my home and I need it to send me an email. However, no matter what I try, it is not connecting to my gmail account to send the notification - or even a simple test message. I have a script that is working for a utility I wrote for work, but that is using the corporate SMTP relay. I'm not going to mix work and personal resources.

I have spent the past 4-5 weeks digging through this and trying several other development languages. All have come up with similar problems.

I have attempted the following:

  • Turned on less secure apps
  • Tried a variety of ports, 25, 465, 587
  • Turned on and off SSL
  • Tried various forms of my username (email address)
  • Turned on 2-factor authentication and created an app password for this utility
  • Attempted to turn on TLS - as one error indicated I needed a starttls command.

With Powershell, I seem to get two common errors. The first one is dependent on the parameters I'm sending to the Send argument - regardless, it fails.

[Screenshot of error - ][1]

I have been on multiple forums and sites. All of the suggested code appears to be all variants of the same structure, as is the working one for my office - just using our own mail system.

回答1:

You should be able to use Send-MailMessage for this. Here's an example for sending an email securely using Gmail's SMTP server (including how to securely store your credential for further use later or in automation):

# Securely enter your Gmail credential
$cred = Get-Credential

# Store it off in a file if you want to reuse it later from the same box
# Password is encrypted and only your user on the same box can decrypt it
$cred | Export-CliXml \Path\To\GmailCredential.xml

# Import the credential from the file later
$cred = Import-CliXml \Path\To\GmailCredential.xml

# Send the mail message (use -BodyAsHtml instead of -Body if you have an HTML body)
$sendMailArguments = @{
  SmtpServer = "smtp.gmail.com"
  Credential = $cred
  UseSsl = $true
  Port = 587
  To = "mailbox@domain.tld"
  From = "youremail@domain.tld"
  Subject = "Subject of the email"
  Body = "Body of the email"
}
Send-MailMessage @sendMailArguments

Note that if you have Multi-Factor Authentication (MFA) set up you will need an app-specific credential to auth to Gmail, which would be a required step as well for setting up an email client for a Gmail account using MFA.

If you can't reach smtp.gmail.com, then it's likely you have a firewall blocking traffic to that server and port.

And if you want to better understand why I'm calling Send-MailMessage with a hashmap of arguments instead of specifying each parameter separately, see the comment discussion on this answer, and read up on Splatting here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6