Skip to content

Instantly share code, notes, and snippets.

@DovieW
Created May 11, 2023 02:26
Show Gist options
  • Save DovieW/2c7e0c5037ccae0df921c32b20fb2f39 to your computer and use it in GitHub Desktop.
Save DovieW/2c7e0c5037ccae0df921c32b20fb2f39 to your computer and use it in GitHub Desktop.
Send Email via PowerShell
$to = "user1@email.com,user2@email.com" # comma separate multiple recipients
$from = "sender@email.com"
$subject = "subject goes here"
$body = "message body goes here"
$smtpServer = "smtp.gmail.com"
$smtpClient = New-Object Net.Mail.SmtpClient($smtpServer, 587) # port 25 for non SSL, port 587 for SSL
$smtpClient.EnableSsl = $true # set to $true for SSL
$smtpClient.Credentials = New-Object System.Net.NetworkCredential("username", "password")
# use this line instead to use credentials of the user running the script
# $smtpClient.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$smtpClient.Send($from, $to, $subject, $body)
#Run it with: powershell -ExecutionPolicy Bypass -File "path/to/send_email.ps1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment