Hey Techies !! We are back with one more interesting article for PowerShell. Today we will cover a script to send an email through PowerShell without the need for an SMTP server. PowerShell is scripting as well as programming language based on .NET. Let’s start the topic “PowerShell Script To Send An Email Without An SMTP Server”

Introduction:
PowerShell has many ways to send emails programmatically. One of the very popular PowerShell cmdlet to send emails is Send-MailMessage but this cmdlet requires you to have an SMTP server handy. Moreover, the SMTP server should allow communication or you might take approval first before using the SMTP server. Hence today we will check out a way to send email in PowerShell without using an SMTP server.
Pre-requisites:
There is only one pre-requisite for this method, which is you should have Microsoft Outlook installed on the machine from which you want to run this script as this script makes an instance of COM object from Outlook.Application .NET class
PowerShell Script To Send An Email Without An SMTP Server
Let’s suppose we need to send an email with an attachment of a CSV file with a date stamp in the subject to our management.
First, we will create a COM object of outlook application in PowerShell like this:
$Outlook = New-Object -ComObject Outlook.Application
Now we defile two variables to hold the Current Date and the file and full path which needs to be attached. Here .\ indicates that the file is in the same directory of the script.
$file = ".\file.csv"
$date = Get-Date -Format g
Now define the following values for recipients and mail subject.
$Mail = $Outlook.CreateItem(0)
$Mail.To = "email@domain.com"
$Mail.Cc = "email@domain.com"
$Mail.Subject = "Sample Report $date EST"
Here comes the main body of an email:
$Mail.Body = "Hi All`n`nPFA the hourly report"
Now we will attach the file and send it.
$Mail.Attachments.Add($file)
$Mail.Send()
Here is the full script. I have also added a try and catch block as sometimes it sends email without attachment, hence I have enclosed that line of code in Try Catch block:
$Outlook = New-Object -ComObject Outlook.Application
$file = ".\file.csv"
$date = Get-Date -Format g
$Mail = $Outlook.CreateItem(0)
$Mail.To = "email@domain.com"
$Mail.Cc = "email@domain.com"
$Mail.Subject = "Sample Report $date EST"
$Mail.Body = "Hi All`n`nPFA the hourly report"
Try
{
$Mail.Attachments.Add($file)
$Mail.Send()
Write-Host "Mail Sent Successfully"
Read-Host -Prompt “Press Enter to exit”
}
Catch
{
Write-Host "File Not Attached Successfully, Please Try Again"
Read-Host -Prompt “Press Enter to exit”
Exit
}
In this way, you can send the emails programmatically in PowerShell without having the need for an SMTP server. Let me know in the below comments if it is helpful for you.
You’ll also like:
- How To Effectively Use PowerShell Variables
- PowerShell Parameters
- PowerShell Script to create a fake BSOD blue screen of death in windows