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.
Our experience has been that IT people who deal with configuring ordinary email accounts for staff have trouble getting all the credentials and security right for DSA Cloud AD in one go. Therefor the testing script.
How to Run PowerShell Scripts This script is a PowerShell / .ps1 file.
The script is based on the Microsoft commands that DSA AD and DSA Cloud AD uses.
$SMTPServerName = 'smtp.office365.com'
[string]$Username = 'user@dataself.com' # <=== Put the SMTP Username here
[string]$Password = '__________' # <=== Put the SMTP password here
$SMTPPasswordEncrypted = ConvertTo-SecureString $Password -as -f
$SMTPCredentialObj = new-object System.Management.Automation.PSCredential $Username, $SMTPPasswordEncrypted
$From = "hello2 <$SMTPServerName>"
write-host $From + " // " + $P_User
[string[]]$EmailTo = "cwilson@dataself.com" #, "me2@cortsoft.com"
write-Host $EmailTo
$Subject = "6 PowershellTest .. (From = $($From)"
$Body= "PowershellTest "
############## WARNING ###############################
# Once "[System.Net.ServicePointManager]::SecurityProtocol" is set the configuration is persistent in a session.
# If testing from Powershell ISE close PowerShell ISE and re-open it.
##############################################################
[System.Net.ServicePointManager]::SecurityProtocol = 'Tls,TLS11,TLS12'
#
Send-MailMessage -From $From -To $EmailTo -Subject $Subject -Body $Body `
-Credential $SMTPCredentialObj -SmtpServer $SMTPServerName -Port 587 -UseSsl -BodyAsHtml
Edit lines 1 - 3 for SMTP server URL, User Name and Password
Older Version
#
# 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
Customize the code in lines 7, 8, and 23 - 26.
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