• Home
  • Topics
    • Office 365
    • Teams
    • SharePoint Online
    • Exchange 2019
    • Exchange 2016
    • Exchange 2013
    • Hybrid
    • Certificates
    • PowerShell
    • Migration
    • Security
    • Azure
  • Blog
  • Podcast
  • Webinars
  • Books
  • About
  • Videos
    • Interview Videos
    • How To Guide Videos
  • Subscribe
    • Facebook
    • Twitter
    • RSS
    • YouTube

Practical 365

You are here: Home / Exchange Server / How to Send SMTP Email Using PowerShell (Part 2)

How to Send SMTP Email Using PowerShell (Part 2)

October 9, 2011 by Paul Cunningham 21 Comments

In the last part of this series we looked at simple techniques for sending email from PowerShell.

  • Part 1 – How to Send SMTP Email Using PowerShell
  • Part 2 – How to Add a Message Body to Emails Sent from Scripts
  • Part 3 – How to Add a HTML Message Body to Emails Sent from Scripts
  • Part 4 – How to Create Formatted HTML Output from Scripts

In this article we’ll take a closer look at how you can create the email message body for emails that you are sending via PowerShell.

In the last article I demonstrated a simple PowerShell script for sending emails that contained the following code, using the SmtpClient .NET object .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#
#.SYNOPSIS
#Sends SMTP email via the Hub Transport server
#
#.EXAMPLE
#.Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "Test email" -Body "This is a test"
#
 
param(
[string]$to,
[string]$subject,
[string]$body
)
 
$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = $to
$messageSubject = $subject
$messageBody = $body
 
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)


Now let’s build on that example by adding more content to the message body of the emails.

Using Command Output as Email Message Content with PowerShell

When running this script anything that we specify with the -Body parameter will be the message body of the email. It could be a text string, or it could even be the output from another PowerShell command. For example:

1
[PS] C:Scripts>.Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "List of Exchange Servers" -Body (Get-ExchangeServer)


The command above would produce an email that looks like this:

How to Send SMTP Email Using PowerShell (Part 2)

Neat trick, but notice how the list of Exchange servers appears as all one string that wraps over two lines? Wouldn’t it be nicer to see the server names displayed in an easier to read list format? Let’s take a look at how you can achieve that.

1
2
3
4
5
6
7
[PS] C:Scripts>[string]$emailbody = ""
 
[PS] C:Scripts>$servers = Get-ExchangeServer
 
[PS] C:Scripts>foreach ($server in $servers) {$emailbody = $emailbody + $server.name + "`r`n"}
 
[PS] C:Scripts>.Send-Email.ps1 -To "administrator@exchangeserverpro.net" -Subject "List of Exchange Servers" -Body $emailbody


So what did I do there? Here are the steps I just followed:

  1. Declare a variable $emailbody as type string. This will be the variable that is passed to the script to be the message body of the email that gets sent.
  2. Used the Get-ExchangeServer cmdlet to retrieve a list of the Exchange servers in the organization into an array of $servers.
  3. Looped through the array using the ForEach-Object (abbreviated to “foreach”) cmdlet and appended each server name to the $emailbody string, including (and this is the important part) a carriage return after each server name.
  4. Ran the script using the $emailbody variable for the -Body script parameter.

The result is an email that looks like this; much better don’t you agree?

How to Send SMTP Email Using PowerShell (Part 2)

Now this is only a demonstration. In reality you probably aren’t going to want to send yourself an email with a list of your Exchange servers, at least not very often.

However you can use the same techniques I’ve just demonstrated to build scripts that email you any command or script output, such as a list of mailboxes with no storage quotas that you have emailed to yourself automatically each month.

Using File Contents as Email Message Content with PowerShell

Another technique for getting content for the message body of an email sent via PowerShell is to use the contents of a file.

For example, I run continuous pings between certain servers to detect any network interruptions that may be occurring. An entry is written to a log file any time a ping times out. Every day I want to receive an email with the results of the previous day’s ping monitoring, so I can do that using a script like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#
#.SYNOPSIS
#Sends daily dropped ping report
#
#.EXAMPLE
#.Send-DroppedPingReport.ps1
#
 
$smtpServer = "ho-ex2010-caht1.exchangeserverpro.net"
$smtpFrom = "reports@exchangeserverpro.net"
$smtpTo = "administrator@exchangeserverpro.net"
$messageSubject = "Dropped ping report"
 
[string]$messagebody = ""
 
$logs = Get-Content C:Logsdroppedpings.log
 
foreach ($log in $logs )
{
$messagebody = $messagebody + $log + "`r`n"
}
 
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)


This is the same technique that was used earlier to create an array and loop through it to add the carriage returns so that the email is formatted nicely. The main difference is the use of Get-Content instead of Get-ExchangeServer.

How to Send SMTP Email Using PowerShell (Part 2)

As you can see it is very simple to create useful, informative emails that are sent by your PowerShell scripts.

Exchange Server Email, PowerShell, Scripting, SMTP

Comments

  1. Tony says

    August 21, 2019 at 4:03 pm

    Hi Paul ,

    i need your help please. our SCOM server is down actually and i need to monitor our replication on Hyperv via powershell command. i found this command :
    Get-VMReplication -ComputerName Hyperv1-HQ.domain .com
    i need the output of this poweshell to be send to me and other colleague via email on a daily basis twice per day, what is the way to do it?
    also can i get the output via email for multiple command for example :

    Get-VMReplication -ComputerName Hyperv1-HQ.domain.com
    Get-VMReplication -ComputerName Hyperv2-HQ.domain.com
    etc…

    thanks a lot.
    Tony.

    Reply
  2. Manoj Kumar Vunnava says

    February 4, 2019 at 12:39 pm

    Hi Paul,

    This is very useful article. Thanks for sharing your knowledge. I’m new to powershell scripting and these articles are crystal clear and very useful.

    I wrote a script for my purpose where it has to trigger an email based on the windows event viewer log. The syntax seems to be correct however I’m getting below error. By any chance do you know what is the issue?

    Error message:
    send-mailMessage : Mailbox unavailable. The server response was: 5.7.60 SMTP; Client does not have permissions to send
    as this sender
    At D:\Powershell Scripts\EventLogNotification.ps1:15 char:2
    + send-mailMessage -From $emailfrom -To $emailto -Subject $emailsubject -Body $em …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
    ion
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

    Reply
  3. Mohammed Ali says

    July 12, 2018 at 4:11 pm

    Hi,

    While sending email through power shell i’m getting display name as email address.
    Is there parameter to set a Display Name for Email to receive instead of email address

    Reply
  4. Vivek Devaraj says

    August 2, 2017 at 2:32 pm

    That helped me. Thank you!

    God bless

    Reply
  5. free ip proxy says

    June 12, 2017 at 11:26 am

    Hello,I check your blog named “PowerShell: How to Add a Message Body to Emails Sent from Scripts” daily.Your story-telling style is awesome, keep doing what you’re doing! And you can look our website about free ip proxy https://proxylistdaily4you.blogspot.com/.

    Reply
  6. Florisz says

    December 9, 2016 at 12:54 am

    @Ricky Valencia
    If you find a solution to put file.txt content in the message body of the email, it would be nice if you share that knowledge here too!

    I and others have the same question.

    Reply
    • Paul Cunningham says

      December 9, 2016 at 9:22 am

      Ingest the file contents to a variable using Get-Content, then use that variable as the message body.

      Eg..

      $filecontent = Get-Content .File.txt

      Then in the Send-MailMessage command use the -Body parameter, e.g.

      -Body $filecontent

      Reply
  7. Sean Noy says

    November 11, 2016 at 4:55 pm

    Hi Eddie,
    You can create a distribution group and send to that address or create a variable and separate the recipients with ,
    example:

    $recipients = “Boss “, “employee1 ”
    Send-MailMessage -To $recipients -Subject ‘New Mail’ -Body $messagebody -SmtpServer $smatpserver -From ‘alerts@domain.com’

    I prefer distribution group as it is easier to manage without editing the script every time

    Best,
    Sean Noy

    Reply
  8. Eddie Chua says

    September 27, 2016 at 6:10 am

    how do i send to multiple recipients?

    Reply
  9. Radhakrishnan Govindan says

    August 22, 2016 at 9:17 pm

    Good Article Paul.. Thank you for sharing..

    Reply
  10. Ricky Valencia Valencia says

    July 2, 2016 at 3:58 am

    Hi Paul,

    Kindly ignore my previous question. I was able to find the solution. I have a second question though, how do i send to multiple recipients?

    Reply
  11. Ricky Valencia Valencia says

    July 2, 2016 at 1:52 am

    HI Paul,

    Great article. I have a question. I have 2 files (txt files) and the contents of those file I want to make it as email message content and send to my email. How can I do that? THank you.

    Reply
    • Tabrez says

      August 2, 2016 at 5:02 pm

      Hi Ricky,

      I have the same problem, I have power shell script for sending backup log file (.txt)in email attachment from task scheduler & its working fine . now I want to send .txt file contents in email body , can you please share the script of this.

      Reply
  12. Anamika says

    June 10, 2016 at 2:21 am

    How to add an attachment. I am using Windows 2012 R2 Server.

    Reply
    • Paul Cunningham says

      June 10, 2016 at 8:51 am

      If you use the Send-MailMessage cmdlet to send emails from PowerShell it has an -Attachments parameter.

      https://technet.microsoft.com/en-us/library/hh849925.aspx

      Reply
      • Steve says

        October 13, 2016 at 3:46 pm

        Very nice (was on the verge of saying elegant). Too bad Send-MailMessage doesn’t do this automatically for txt files (ie treat it as/is).

        Reply
  13. Sourav M says

    April 9, 2016 at 8:45 am

    Very nice Blog. Thanks much.

    Reply
  14. Ted Wagner says

    April 21, 2015 at 12:23 am

    Paul,

    Here’s a bit of another challenge. In Exchange, you can create a foreign connector with an address space with a transport rule for routing. An example would be with RightFax.

    new-foreignconnector -name “RightFaxConnector” -addressspaces (“RFAX:*;1″,”FAX:*;1”)

    So, in the example of RightFax, you address for sending would be FAX:Name@1112223333

    To send from an email client, you would simply include brackets: [FAX:Name@1112223333]

    However, with Powershell, the send-mailmessage expects a static format of recipient@domain.com

    However, if you try to use send-mailmessage, the cmdlet won’t recognize the address space and the recipient is invalid.

    Neither the Powershell 1.0 nor the Powershell 2.0 methods work.

    Is it possible to send an email using a different address space with Powershell?

    Reply

Leave a Reply Cancel reply

You have to agree to the comment policy.

Recent Articles

  • Microsoft Launches Group Ownership Governance Policy
  • Making the Case for Identity Governance in Azure Active Directory
  • Prepare an Office 365 migration plan assessment using PowerShell
  • Microsoft Releases May 2022 Exchange Server Security Updates
  • New Future of Work for Microsoft 365, IOT and more: Practical 365 Podcast S3 Ep. 2

Copyright © 2022 Quadrotech Solutions AG · Disclosure · Privacy Policy
Alpenstrasse 15, 6304 Zug, Switzerland