Project Objective:
Send simple e-mail with or without credentials, with or without SSL encryption.

Problem to be solved:
My current scripts do not account for SMTP authorization being required on send. This script can be used as a standalone script or with minor changes a function.

Future Versions:
Since the primary purpose of this script was versatility I need to re-add the ability to send attachments. I removed the incomplete functionality from because at the moment I do not need to send attachments.

Notes:

This script (or one like it) was originally written by Rodney Fisk – xizdaqrian@gmail.com with aid from: Christian @ StackOverflow.com and Core @ StackOverflow.com

#
# Send-Email.ps1
#
# Written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com)
#
# If you edit please keep my name as an original author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor, runner, user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# 2011.07.05 ver 2.0
#
# Summary:
# Send simple e-mail with or without credentials, with or without SSL encryption.
# The primary purpose of this script was versatility.
# -----------------------------------------------------------------------
# Usage:
# This script can be used as a standalone script or with minor changes a function.
# Example:
# Send-Email.ps1 -from aaron.wurthmann@domain.local -to aaron_wurthmann@domain.local -Subject "Test Subject #42" -Body "Nice body!" -Server smtp.domain.local -Port 587 -User USERNAME -Pass PASSWORD -SSL True
# -----------------------------------------------------------------------
# Orgin:
# This script (or one like it) was originally written by Rodney Fisk - xizdaqrian@gmail.com
#     with aid from: Christian @ StackOverflow.com and Core @ StackOverflow.com
# -----------------------------------------------------------------------



# Mandatory = $true means that the parameter is required and if not included you will be prompted for it.
# Alias is just that, an alias to a parameter verses using the actual parameter name. I.G. "From" vs. "EmailFrom"
param(
    [Parameter(Mandatory = $true,
        Position = 0,
        ValueFromPipelineByPropertyName = $true)]
        [Alias('From')]
    [String]$emailFrom,

    [Parameter(Mandatory = $true,
        Position = 1,
        ValueFromPipelineByPropertyName = $true)]
        [Alias('To')]
    [String[]]$emailRecipients,

    [Parameter(Mandatory = $true,
        Position = 2,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Subject' )]
    [String]$emailSubject,

    [Parameter(Mandatory = $true,
        Position = 3,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Body' )]
    [String]$emailBody,

    [Parameter(Mandatory = $true,
        Position = 4,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Server' )]
    [String]$SMTPServer,

    [Parameter(Mandatory = $false,
        Position = 5,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Port' )]
    [Int]$SMTPPort=25,
    
    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'User' )]
    [string]$SMTPUser,

    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Password' )]
    [string]$SMTPPass,
    
    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'SSL' )]
    $SMTPSSL
)

#Create an SMTP client using .NET
$SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, $SmtpPort ) 

#If SSL is needed per parameter above enable SSL on the SMTP client
if ($SMTPSSL) { $SMTPClient.EnableSSL = $true }
#If a Username and Password have been added per parameter above add to SMTP client
if ($SMTPUser -and $SMTPPass) { $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $SMTPUser, $SMTPPass ) }

#Create new message to be sent
$emailMessage = New-Object System.Net.Mail.MailMessage
#Add who will send the message to the message envelope
$emailMessage.From = $emailFrom
#Add Recipient(s) to the message envelope (Note that this method adds all recipients to the same message)
foreach ( $emailRecipient in $emailRecipients ) { $emailMessage.To.Add( $emailRecipient ) }

#Add Subject to message envelope
$emailMessage.Subject = $emailSubject
#Add Body to message envelope
$emailMessage.Body = $emailBody

#Using SMTP client send email message
$SMTPClient.Send( $emailMessage )
#Clear email message (helps clean up memory use)
$emailMessage.Dispose()
Be Sociable, Share!