Sending Email With Attachments In PowerShell
January 19th, 2010
Here's an example on how to send email with attachments via PowerShell:
POWERSHELL:
-
# A Mailer script that makes use of System.Net to send email with attachments
-
#
-
# Sample usage:
-
# PS C:\> Send-Mail-With-Attachment 'email@domain.com' 'Hello world!' 'Filename.txt'
-
-
function global:Send-Mail-With-Attachment($to, $subject, $file){
-
-
$filenameAndPath = (Resolve-Path .\$file).ToString()
-
$from = 'Automated Powershell Mailer'
-
-
[void][Reflection.Assembly]::LoadWithPartialName('System.Net') | out-null
-
-
$message = New-Object System.Net.Mail.MailMessage($from, $to, $subject, $subject)
-
$attachment = New-Object System.Net.Mail.Attachment($filenameAndPath, 'text/plain')
-
$message.Attachments.Add($attachment)
-
-
$smtpClient = New-Object System.Net.Mail.SmtpClient
-
$smtpClient.host = 'mail.domain.com'
-
$smtpClient.Send($message)
-
}
Contribute, view, or download the script here: Mailer.ps1