sthMailProfile - is a module, containing four functions for creating mail profiles and using them to send mail messages.
It contains following functions:
Send-sthMailMessage - Function sends mail message using settings from the profile specified.
New-sthMailProfile - Function creates mail profile containing specified settings.
Get-sthMailProfile - Function gets existing mail profiles and displays their settings.
Remove-sthMailProfile - Function removes specified profiles.
Profile is an xml file, containing settings, such as: From, To, Credential, SmtpServer, Port, UseSSL, Encoding, BodyAsHtml, CC, BCC, DeliveryNotificationOption, and Priority.
You can create the profile by using the New-sthMailProfile function with the -ProfileName or -ProfileFilePath parameter.
-ProfileName parameter creates an .xml file with the specified name under the Profiles folder in the module's directory.
-ProfileFilePath parameter accepts path and name of the file, i.e. C:\Folder\file.xml, and creates it in the specified location.
You can install sthMailProfile module from PowerShell Gallery:
Install-Module sthMailProfile
The commands send mail message using previously created profile.
The first command gets the result of the Get-Process cmdlet and assigns it to the $ps variable.
The second command sends it using previously created profile "MailProfile".
$ps = Get-Process
Send-sthMailMessage -ProfileName "MailProfile" -Subject "Process List" -Message $ps
The first command gets the result of the Get-Process cmdlet and assigns it to the $ps variable.
The second command sends it using previously created profile "MailProfile".
The command uses positional parameters.
$ps = Get-Process
Send-sthMailMessage "MailProfile" "Process List" $ps
The commands send mail message using profile file at the path specified.
The first command gets the result of the Get-Process cmdlet and assigns it to the $ps variable.
The second command sends it using previously created profile file SomeProfile.xml located in the C:\Profiles directory.
$ps = Get-Process
Send-sthMailMessage -ProfileFilePath C:\Profiles\SomeProfile.xml -Subject "Process List" -Message $ps
The command gets the result of the Get-Process cmdlet and sends it using previously created profile "MailProfile".
It uses pipeline for sending message content to the function.
Get-Process | Send-sthMailMessage -ProfileName "MailProfile" -Subject "Process List"
The command gets the result of the Get-Process cmdlet and sends it with specified files as attachments using previously created profile "MailProfile".
It uses pipeline for sending message content to the function.
Get-Process | Send-sthMailMessage -ProfileName "MailProfile" -Subject "Process List" -Attachments "file1.txt, file2.txt"
The command creates mail profile with name "MailProfile", which contains settings: From, To and SmtpServer.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com
The command creates mail profile with name "MailProfile", which contains settings: From, To and SmtpServer using positional parameters.
New-sthMailProfile "MailProfile" source@domain.com destination@domain.com smtp.domain.com
The command creates the profile file with the name SomeProfile.xml in the C:\Profiles directory.
New-sthMailProfile -ProfileFilePath "C:\Profiles\SomeProfile.xml" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com
The command creates mail profile with name "SendEmpty" and settings: From, To, SmtpServer, Subject, Port, UseSSL, Encoding, BodyAsHtml, CC, BCC, DeliveryNotificationOption, and Priority.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -Subject "TheSubject" -Port 587 -UseSSL -Encoding UTF-8 -BodyAsHtml -CC cc@domain.com -BCC bcc@domain.com -DeliveryNotificationOption OnSuccess -Priority High
The command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer, UserName and Password.
The -Password parameter is not used, so the command requests for it.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -UserName user@domain.com
Type the password:
Since SecureString uses DPAPI, if you create mail profile containing credential without -StorePasswordInPlainText parameter, it can only be used on the computer it was created on and by the user account that created it.
The command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer, UserName and Password.
Password is specified as string.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -UserName user@domain.com -Password 'password'
Since SecureString uses DPAPI, if you create mail profile containing credential without -StorePasswordInPlainText parameter, it can only be used on the computer it was created on and by the user account that created it.
The commands create a new profile with credential by specifying password as secure string.
The first command creates secure string from plain text password and assigns it to the $Password variable.
The second command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer, UserName and Password.
Password is specified as secure string.
$Password = ConvertTo-SecureString -String 'password' -AsPlainText -Force
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -UserName user@domain.com -Password $Password
Since SecureString uses DPAPI, if you create mail profile containing credential without -StorePasswordInPlainText parameter, it can only be used on the computer it was created on and by the user account that created it.
The commands create a new profile by specifying credential as PSCredential object.
The first command creates secure string from plain text password and assigns it to the $Password variable.
The second command creates PSCredential object using 'UserName' and previously created password as arguments.
The third command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer and Credential.
$Password = ConvertTo-SecureString -String 'password' -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'UserName', $Password
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -Credential $Credential
Since SecureString uses DPAPI, if you create mail profile containing credential without -StorePasswordInPlainText parameter, it can only be used on the computer it was created on and by the user account that created it.
The command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer and Credential.
Credential parameter value is specified as an array of two elements.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -Credential @('UserName', 'password')
Since SecureString uses DPAPI, if you create mail profile containing credential without -StorePasswordInPlainText parameter, it can only be used on the computer it was created on and by the user account that created it.
The command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer, UserName and Password.
Since the -StorePasswordInPlainText parameter is used, password will be stored in plain text.
It allows you to use the profile on computers other than one it was created on, and under different user accounts, other than it was created by.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -UserName user@domain.com -Password 'password' -StorePasswordInPlainText
The first command creates mail profile with name "MailProfile" and settings: From, To, SmtpServer, and Subject.
The second command sends mail message using subject from the profile.
The third command sends mail message using subject defined by the Send-sthMailMessage -Subject parameter.
New-sthMailProfile -ProfileName "MailProfile" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -Subject "TheSubject"
Send-sthMailMessage -ProfileName "MailProfile" -Message "TheMessage"
Send-sthMailMessage -ProfileName "MailProfile" -Subject "AnotherSubject" -Message "TheMessage"
The first command creates mail profile with name "SendEmpty" and settings: From, To, and SmtpServer.
The second command creates mail profile with name "NoNotSendEmpty" and settings: From, To, SmtpServer, and DoNotSendIfMessageIsEmpty.
The third command tries to send mail message with empty body. The message will be sent.
The fourth command tries to send mail message with empty body. The message will not be sent.
New-sthMailProfile -ProfileName "SendEmpty" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com
New-sthMailProfile -ProfileName "DoNotSendEmpty" -From source@domain.com -To destination@domain.com -SmtpServer smtp.domain.com -DoNotSendIfMessageIsEmpty
'' | Send-sthMailMessage -ProfileName "SendEmpty" -Subject "TheSubject"
'' | Send-sthMailMessage -ProfileName "DoNotSendEmpty" -Subject "TheSubject"
The command gets all profiles and displays their settings.
Get-sthMailProfile
ProfileName : MailProfile
From : source@domain.com
To : {destination@domain.com}
PasswordIs : NotExist
SmtpServer : smtp.domain.com
ProfileName : MailProfile2
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
PasswordIs : PlainText
SmtpServer : smtp.domain.com
ProfileName : MailProfile3
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
PasswordIs : Secured
SmtpServer : smtp.domain.com
The command gets profile "MailProfile" and displays its settings.
Get-sthMailProfile MailProfile
ProfileName : MailProfile
From : source@domain.com
To : {destination@domain.com}
PasswordIs : NotExist
SmtpServer : smtp.domain.com
The command gets profiles which name starts with "Mail" and displays their settings.
Get-sthMailProfile -ProfileName Mail*
ProfileName : MailProfile
From : source@domain.com
To : {destination@domain.com}
PasswordIs : NotExist
SmtpServer : smtp.domain.com
ProfileName : MailProfile2
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
PasswordIs : PlainText
SmtpServer : smtp.domain.com
ProfileName : MailProfile3
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
PasswordIs : Secured
SmtpServer : smtp.domain.com
The command gets settings from the profile file located at C:\Profiles\SomeProfile.xml.
Get-sthMailProfile -ProfileFilePath C:\Profiles\SomeProfile.xml
ProfileName : SomeProfile
From : source@domain.com
To : {destination@domain.com}
PasswordIs : NotExist
SmtpServer : smtp.domain.com
The command gets all profiles and displays their settings including password.
Get-sthMailProfile -ShowPassword
ProfileName : MailProfile
From : source@domain.com
To : {destination@domain.com}
PasswordIs : NotExist
SmtpServer : smtp.domain.com
ProfileName : MailProfile2
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
Password : password
PasswordIs : PlainText
SmtpServer : smtp.domain.com
ProfileName : MailProfile3
From : source@domain.com
To : {destination@domain.com}
UserName : user@domain.com
Password : password
PasswordIs : Secured
SmtpServer : smtp.domain.com
The command removes the profile "MailProfile".
Remove-sthMailProfile -ProfileName "MailProfile"
The command removes the profiles which name starts with "Mail".
Remove-sthMailProfile -ProfileName "Mail*"
The command removes all profiles.
Remove-sthMailProfile -ProfileName *
The command removes the profile file with the name SomeProfile.xml in the C:\Profiles directory.
Remove-sthMailProfile -ProfileFilePath C:\Profiles\SomeProfile.xml