Cyprich.com

Occasionally Relevant Insights from a Software Builder

By

SendMail Function for PowerShell

The SendMail function for PowerShell will send an e-mail through an SMTP server. I’m currently using it for backing up a web server to a virtual machine and sending a confirmation by e-mail on the status of the backup.

Usage is very simple:

          SendMail Host From To Subject Message UserName Password

The Host is the mail server address, i.e. smtp.mail.com. The From and To are the sender and receiver of the e-mail. The Subject and Message are the title and contents of the e-mail. If authentication is required, then enter values for the UserName and Password, otherwise make them blank using single quotes, i.e. ‘’.

The function will return a boolean value: true if the mail was sent successfully, false if it couldn’t be sent. See example in this post.

 

SendMail Code

The following is the code for the SendMail function which can be pasted in your PowerShell application.

[== Code ==============================================]

function SendMail ([string] $sHost, [string] $sFrom, [string] $sTo, [string] $sSubject, [string] $sMessage, [string] $sUserName, [string] $sPassword)

####################################################################################################
# PURPOSE:
#   Sends an e-mail to an SMTP server. It will also authenticate to the server if a user name and
#   password are sent to the function. If authentication is not used, leave $sUserName and $sPassowrd
#   blank, i.e. ”.
#
# PARAMETERS:
#   $sHost (in) – host name of mail server
#   $sFrom (in) – e-mail address of mail sender
#   $sTo (in) – e-mail address of mail recipient
#   $sSubject (in) – subject title of message
#   $sMessage (in) – message sent to recipient
#   $sUserName (in) – user name for authentication
#   $sPassword (in) – password for authentication
#
# RETURN:
#   True – mail was sent succesfully
#   False – mail was not sent
####################################################################################################

{
  [bool] $bSuccess = 1                 # was mail sent without problems?

  $SmtpClient = new-object system.net.mail.smtpClient 
  $smtpclient.Host = $sHost

  # Authenticate only if a user name and password are set.
  if (($sUsername -ne ”) -band ($sPassword -ne ”))
  {
    $Credentials = new-object System.Net.networkCredential

    $Credentials.domain = ""
    $Credentials.UserName = $sUserName
    $Credentials.Password = $sPassword
    $SMTPClient.Credentials = $Credentials
  } # if (($sUsername -ne ”) -band ($sPassword -ne ”))

  # Send the mail.
  try
  {
    $smtpclient.Send($sFrom, $sTo, $sSubject, $sMessage)
  }
  catch
  {
    $bSuccess = 0
  }
  $bSuccess
} # SendMail

[== Code ==============================================]

 

Example of the SendMail Function

This is an example of how the SendMail function should be used.

[== Code ==============================================]

$bSent = SendMail ‘smtp.mail.com’ ‘sender@website.com’ ‘recipient@website.com’ ‘Test’ ‘This is a test.’ ‘sender@website.com’ ‘abc123’

if ($bSent)
{
  "Mail sent successfully."
}


else
{
  "Mail not sent."
}

[== Code ==============================================]

 

You can download the SendMail code and example here.

2 Responses to SendMail Function for PowerShell

  1. Pingback: SendMail Function for PowerShell | cyprich.com | www.erasedmail.com

  2. Gautam says:

    How to add 2 email addresses in -SendMail:$true -MailFrom:Gautam.Verma@domain.com -MailTo:Admin@domain.com -MailServer:smtp.domain.com

Leave a Reply

Your email address will not be published. Required fields are marked *