SendMail Function for PowerShell

Posted by Johan Cyprich on 27 Jul 2010 | Tagged as: Programming

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.



Tweet This Tweet This Post!

Related posts:
    Getting the PowerShell Version
    Setting Permissions to Run PowerShell Scripts
    Upgrading to Windows PowerShell 2.0 from Community Technology Preview
    Creating a Safety Net for WordPress Plugin Integrations

Share this post:

del.icio.us:SendMail Function for PowerShell digg:SendMail Function for PowerShell spurl:SendMail Function for PowerShell wists:SendMail Function for PowerShell simpy:SendMail Function for PowerShell newsvine:SendMail Function for PowerShell blinklist:SendMail Function for PowerShell furl:SendMail Function for PowerShell reddit:SendMail Function for PowerShell fark:SendMail Function for PowerShell blogmarks:SendMail Function for PowerShell Y!:SendMail Function for PowerShell smarking:SendMail Function for PowerShell magnolia:SendMail Function for PowerShell segnalo:SendMail Function for PowerShell gifttagging:SendMail Function for PowerShell

Follow Me:

Did you find this post interesting and useful? You can keep up to date on this blog by subscribing to my RSS feed, or you can have new posts sent to you by e-mail. You can also follow me on Twitter.


One Response to “SendMail Function for PowerShell”

  1. on 27 Jul 2010 at 6:17 pm 1.SendMail Function for PowerShell | cyprich.com | www.erasedmail.com said …

    [...] original post here: SendMail Function for PowerShell | cyprich.com power, smtp, virtual-machine, web-server, [...]

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply

CommentLuv badge