Send-SMTP PowerShell Script

I’ve been creating scripts to automate administrative tasks for myself and clients. PowerShell scripts are a very nice way of accomplishing the needed automation when working with Exchange 2010.

One such task often involves gathering information for monitoring purposes. To facilitate the sharing of information it is always helpful to be able to send email.

I have been using this script for a while now that allows for just that. The code is below or you can download the .ps1 file: Send-SMTP.ps1. Enjoy!

<#  
.SYNOPSIS  
    Sends Email Message
.DESCRIPTION  
    Allows the generation and submission of a SMTP message to an MTA (email server).
    
.NOTES  
    Version      : 1.0 - 04/26/2010 - initial version
    Rights Req'd : Local Server Admin
    Sch Task Req : No
    Author       : Robert Durkin
    Email        : rdurkin (at) ehloworld.net
    Blog         : http://ehloworld.net
    Disclaimer   : Don't blame me if this breaks your stuff.  Please don't Spam!
    References   : 
.LINK  
	Online Version: http://ehloworld.net/?p=60
.EXAMPLE
	.\Send-SMTP.ps1 -From "sender@domain.net" -To "recipient@domain.net" -Subject "Blog Post" -Body "Ehlo World!" -Server mailer.domain.com
.INPUTS

#>

#Requires -Version 2.0

PARAM(
	[Parameter(Mandatory = $True,valueFromPipeline=$True)][string] $From,
	[Parameter(Mandatory = $True,valueFromPipeline=$True)][string] $To,
	[Parameter(Mandatory = $True,valueFromPipeline=$True)][string] $Subject,
	[Parameter(Mandatory = $True,valueFromPipeline=$True)][string] $Body,
	[Parameter(Mandatory = $True,valueFromPipeline=$True)][string] $Server,
	[Parameter(Mandatory = $false,valueFromPipeline=$True)] $File
) #end param

#Create Mailer Object and assign values
$MTA = New-Object Net.Mail.SMTPclient($Server)
$Msg = New-Object Net.Mail.MailMessage

$Msg.From = $From
$Msg.To.Add($To)
$Msg.Subject = $Subject
$Msg.Body = $Body

#Attach File, if applicable
IF ($File -ne $Null)
{
	$File = New-Object Net.Mail.Attachment($File)
	$Msg.Attachments.Add($File)
}

$MTA.send($Msg)
Posted in PowerShell

4 comments on “Send-SMTP PowerShell Script

  • $MTA.send($Msg) does not seem to work since it is looking for 4 arguments. I am using the PS released with Vista, i.e. v 1.0.

    Is a newer version of PS needed for $MTA.send to assign the $Msg properties automatically?

    Also specifying the Smtp port number may be necessary since carriers like Comcast block the default port 25. I need to use Port 587 myself.

  • Thanks for this, most helpful, I would like to capture any errors that the email generates, i.e could not relay etc…. but cant see how, could you give me any pointers?

  • Nice writeup. If you want to send multiple attachments you will need to iterate through them to add them. So assuming that $File is a list of files separated by commas, replace the scriptblock following your “IF ($File -ne $Null)” statement with:

    $File = $File.split(“,”)
    For($i=0;$i-lt $File.Count;$i++) {
    $Attachment = New-Object Net.Mail.Attachment($File[$i])
    $Msg.Attachments.Add($Attachment)
    }

    Or, if you’re living in a PowerShell 2.0 world you can also use the Send-MailMessage cmdlet.

Leave a Reply