Skip to content

Instantly share code, notes, and snippets.

@frandi
Last active June 11, 2021 09:14
Show Gist options
  • Save frandi/d9dbbc0bd3873f0bf9cf727bf8c54c27 to your computer and use it in GitHub Desktop.
Save frandi/d9dbbc0bd3873f0bf9cf727bf8c54c27 to your computer and use it in GitHub Desktop.
Send email from PowerShell (SMTP Test)
# install the module (it only needs to be done once)
Install-Module -Name "Send-MailKitMessage" -Scope CurrentUser
$SMTPServer = "smtp.gmail.com"
$Port = 587
$Username = ""
$Password = ""
$Credential=[System.Management.Automation.PSCredential]::new($Username, (ConvertTo-SecureString -String $Password -AsPlainText -Force))
$From = "sender@gmail.com"
$RecipientList=[MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]"recipient@gmail.com")
$CCList=[MimeKit.InternetAddressList]::new()
$CCList.Add([MimeKit.InternetAddress]"cc@gmail.com")
$Subject = "SMTP Test"
$TextBody = "This email was sent from PowerShell"
$HTMLBody = "This email was sent from <b>PowerShell</b>"
$AttachmentList=[System.Collections.Generic.List[string]]::new()
$AttachmentList.Add("c:\path\to\attachment")
Send-MailKitMessage -SMTPServer $SMTPServer -Credential $Credential -Port $Port -From $From -RecipientList $RecipientList -Subject $Subject -TextBody $TextBody
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment