Send email gmail powershell
You can use the Send-MailMessage
cmdlet in PowerShell to send an email using your Gmail account. Here's an example:
$from = "[email protected]"
$to = "[email protected]"
$subject = "Test email from PowerShell"
$body = "This is a test email sent from PowerShell using Gmail."
$smtpServer = "smtp.gmail.com"
$smtpPort = 587
$cred = New-Object System.Net.NetworkCredential($from, (ConvertTo-SecureString "your-gmail-password" -AsPlainText -Force))
Send-MailMessage -From $from -To $to -Subject $subject -Body $body -SmtpServer $smtpServer -Port $smtpPort -Credential $cred
Make sure to replace the placeholders with your actual Gmail username, password, and recipient email address.
Note:
- You need to enable less secure apps in your Google account settings to use this method. You can do this by going to your Google Account settings, clicking on "Security", and then toggling the switch next to "Less secure app access" to the "On" position.
- You may also need to generate an App Password for your Gmail account if you have 2-Step Verification enabled. You can do this by going to your Google Account settings, clicking on "Security", and then clicking on "App passwords". Follow the instructions to generate a password.
- Make sure to use the correct SMTP server and port for your Gmail account. The default values are
smtp.gmail.com
and587
, but you may need to use a different server and port depending on your account settings.
Also, you can use the Send-Gmail
module which is a PowerShell module that allows you to send emails using Gmail. You can install it by running the following command:
Install-Module -Name Send-Gmail
Then you can use the following command to send an email:
Send-Gmail -From "[email protected]" -To "[email protected]" -Subject "Test email from PowerShell" -Body "This is a test email sent from PowerShell using Gmail."
Make sure to replace the placeholders with your actual Gmail username and password.