Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Last active December 27, 2018 13:36
Show Gist options
  • Select an option

  • Save gitfvb/c3f39d8671a737531721477f9c07151b to your computer and use it in GitHub Desktop.

Select an option

Save gitfvb/c3f39d8671a737531721477f9c07151b to your computer and use it in GitHub Desktop.
Send a mail via powershell without installing any additional addon. Only SMTP required.
################################################
#
# PREPARATION / ASSEMBLIES
#
################################################
# Load scriptpath
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") {
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
} else {
$scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
}
cd $scriptPath
################################################
#
# SAVE PASSWORD
#
################################################
# save password with
(Get-Credential).password | ConvertFrom-SecureString > MailPW.txt
################################################
#
# PREPARATION / ASSEMBLIES
#
################################################
# Load scriptpath
if ($MyInvocation.MyCommand.CommandType -eq "ExternalScript") {
$scriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
} else {
$scriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
}
cd $scriptPath
################################################
#
# SEND MAIL
#
################################################
# like in https://www.windowspro.de/script/send-mailmessage-e-mails-versenden-powershell
# read encrypted password
$pw = Get-Content .\MailPW.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential "faststats@apteco-faststats.de", $pw
Send-MailMessage -To "receiver@example.com"`
-Subject "Low space warning"`
-Body "Please check the memory of the server"`
-SmtpServer "smtp.server.de"`
-From "username@example.de"`
-UseSsl -Port 587 -Credential $cred -encoding ([System.Text.Encoding]::UTF8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment