Send Email via Powershell

Sending email is a pretty mundane task when you are a system admin. Create script that will send the mail without you opening the outlook is pretty cool and you would never be late to send that one report.

below script will allow you to send a mail via powershell.

$TodaysDate = (Get-Date -Format "yyyyMMddHHmmss")
$fileName = "attachments"
New-Item "c:\temp\$fileName" -ItemType file
$file = "c:\temp\$fileName"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$msg.From = 'sender's email address'
$msg.To.Add('username@domain.com')
$msg.Subject = "Report"
$msg.Body = "body of the email."
$msg.Attachments.Add($att)
$smtpServer = "smtp serverl"


$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)

Pretty cool isn’t it? i have been using this script for pretty much all of my automated reports to send out to management.

I do also have an Mail engine which basically shoots out email in designated time to specific people with distinct reports, which can be uploaded based on the request.

Published by iamfazul

Author of the site

One thought on “Send Email via Powershell

Leave a comment