Script for Testing Email (Send-MailMessage)
This script is based on the code used by DataSelf DSA AD and DSA Cloud AD software to send outgoing emails.
How to Run PowerShell Scripts This script is a PowerShell / .ps1 file.
The script just replicates what DSA AD and DSA Cloud AD does – the script calls the same Microsoft provided commands that DSA AD uses.
IT people can refer to Microsoft’s documentation on PowerShell’s Send-MailMessage commandlet.
#
# Fill in the appropriate values for:
# * The $P_User and $P_Password variables
# * Values for the various Send-MailMessage command.
#
####### Credentials ####################################################
[string]$P_User = 'dsbi@dataself.com' # <=== Put the SMTP Username here
[string]$P_Password = 'xxxxxxxxx' # <=== Put the SMTP password here
if ( [string]::IsNullOrEmpty( $P_Password) ) {
$SMTPPasswordEncrypted = New-Object System.Security.SecureString
} else {
$SMTPPasswordEncrypted = ConvertTo-SecureString $P_Password -as -f
}
$SMTPCredentialObj = new-object System.Management.Automation.PSCredential `
$P_User, $SMTPPasswordEncrypted
#----------------------------------------------------------------------
# [System.Net.ServicePointManager] ? --- command may not be required
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
#----------------------------------------------------------------------
Send-MailMessage -To 'cwilson@dataself.com' `
-From 'cwilson@dataself.com' `
-Subject 'Send-MailMessage Test msg' `
-SmtpServer 'Smtp.gmail.com' -Port 587 -UseSsl -Credential $SMTPCredentialObj
Notes:
PowerShell uses the backtick symbol (
`
) as the continuation character.
On many keyboards the backtick is collocated with the tilda (~
) key.For more on the Send-MailMessage command see Send-MailMessage documentation from Microsoft.
Usage
Copy the script above and save as a .ps1 file. Then run as a PowerShell script: How to Run PowerShell Scripts
The script just replicates what DSA AD and DSA Cloud AD does – the script calls the same Microsoft provided commands that DSA AD uses.
Sending HTML Format Emails
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-5.1 Microsoft’s documentation on PowerShell’s Send-MailMessage command.
https://www.sharepointdiary.com/2015/06/send-mailmessage-powershell-body-html-format.html