In this post, I will talk about commonly used parameters of the Send-MailMessage PowerShell cmdlet and a few examples of how to use them.

Sitaram Pamarthi

Sitaram Pamarthi is working as a Windows Engineer and his special fields of interest are PowerShell, Active Directory, Exchange, and virtualization.

Update: We have a new post that explains how to send an email with PowerShell in more detail.

Most of the automations that system administrators write generally involve sending email at some point in the script, whether to report errors or to send consolidated output. Given the high usage of this functionality, I would like to describe some of the common use cases of the Send-MailMessage cmdlet (a new cmdlet introduced in PowerShell 2.0).

Send-MailMessage parameters ^

For any cmdlet, we first need to understand the parameters that we should pass to it to get the desired functionality/output. Like any other cmdlet, Send-MailMessage also has both mandatory and optional parameters. I will talk about the ones that are used most.

-FROM: Every email needs a “from” address. Without this, email cannot be sent. Though it is a required parameter, the email ID doesn’t necessarily have to be a valid one. You can use nothing@nothing.com and the cmdlet will still work.

-TO: This is also a mandatory parameter. You can supply a single or multiple email addresses, and you can also specify the name associated with the email address (for example, -To “Sitaram Pamarthi <sitaram @4sysops.com>”).

-SMTPServer: You need to provide the name of the SMTP server through which you want to send the email. If you are in doubt, ask your email administrator for the server to use. If this parameter is not specified, PowerShell tries to pick the value from the $PSEmailServer preference variable, if this variable is set.

-CC and -BCC: You know what they are. If you would like to CC or BCC somebody in the email, pass their email addresses to these parameters.

-Attachments: This parameter takes the full path of the file(s) that you want to attach to the email.

-Subject: This is a simple string that represents the subject of your email.

-Body: This string specifies the body of your email message.

Send-MailMessage examples ^

We have seen some of the frequently used parameters. Now let me take you through some examples, which will come in handy when writing your scripts.

Send email to one recipient

Send email to multiple recipients in To and CC

Send email to one recipient with an attachment

Send email to one recipient with multiple attachments

Send email via an SMTP relay server that requires authentication

The above command will prompt for the credentials and send the email after the credentials are validated.

Send status of all services in a server as an email

Send high-priority email

These use cases are just a few examples. You can do much more with this cmdlet, such as send emails over TLS channels (-UseSSL parameter) or request delivery receipts for emails that you send (-DeliveryNotificationOption parameter).

To obtain help for this cmdlet, type the following command in your PowerShell window:

Update: We have a new post that explains how to send an email with PowerShell in more detail.

Join the 4sysops PowerShell group!

7+

Users who have LIKED this post:

  • avatar
Share
20 Comments
  1. Karthik 3 years ago

    How do I send mail when I do not know the SMTP server host, but know only the LDAP host name

    0

    • Michael Pietroforte 3 years ago

      Without SMTP server you can't send an email.

      1+

    • Huy M Le 2 years ago

      If you have an email account from anywhere then you can send email.

      To find out the SMTP server you just go to MXtoolbox.com,

      from there enter in you domain name (ie mysmtp.com) where your email is (myname@mysmtp.com).  This will return you the  MX server record (SMTP).  From there you just copy that address as your SMTP server.  You may have to use (credential) option on the PowerShell Command to specify smtp server and have to input your username/password (email + password).  That should work fine.

      0

  2. addison 3 years ago

    Thank you.  This was very helpful.

    0

  3. Sam 3 years ago

    How do I ensure that the from name field that the recipient sees is customized as per the value I provided.
    I have a requirement where I send emails to multiple people but I don't want them to reply to me and spam my mailbox. I tried using 'donotreply@mydomain.com' in the from field but it still ends up with the recipient with my actual email id.

    I have powershell 4 and our smtp server is google (enterprise gmail service).

    0

    • Michael Pietroforte 3 years ago

      Try another SMTP server. I guess Google's mail server replaces the contents of the from field with your email address. If you ask, they might tell you that they do this to prevent spamming. In reality they want you to pay for another mailbox.

      0

  4. Rick 3 years ago

    I am trying to add a internet header (self-created one, call it x-abc) how would I code that in powershell in the send-mailmessage?

    0

  5. Rick 3 years ago

    This is Rick, what I am trying to do is to add a custom x-header into an outgoing e-mail using the send-mailmessage. The e-mail will be sent via the task scheduler in Win 2012.

    0

  6. Shalaka 2 years ago

    hi, I am using a variable that would have  the list of files to be attached (dynamically generated bases on some other logic) that I want to send to the -Attachements...but it does not seem to work.

    My variable:

    $attachment = "C:\......pdf","C:\.....pdf"

    Then in the send mail I have

    Send-MailMessage `
    -To $emailarray `
    -From ..
    -SmtpServer ...
    -Subject "$subject" `
    -Body "$body" `
    -Attachments $attachment `

    0

  7. Eman 2 years ago

    how can i select 2 multiple random attachments from a folder?

    0

  8. Mike D 2 years ago

    Instead of listing each email address in the -To parameter, is it possible to set this up to read a .txt file of email addresses, or even better a .csv list of emails?

    0

    • Michael Pietroforte 2 years ago

      If your CSV file looks like this:

      you could use a script that looks like this:

      2+

  9. Terry Bennett 1 year ago

    how can I do error checking on Send-MailMessage?

    IF the message was sent successfully, continue, otherwise, do it again, etc, etc.

    Thanks,

    T

     

    0

  10. kumaraswamy 8 months ago

    while sending  mail through powershell , im getting below error:

    Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

     

    0

  11. Chris 6 months ago

    I'm writing a script that does 3 things.

    1. Scans the D:\ drive for viruses
    2.  Scans the D:\  drive for medical records
    3.  Sends out an email with the same subject and slightly different body but the recipient will change. How can I specify a different recipient when running the script
    4.  Bonus points if you also can share how I'd be able to edit the body of the email directly in the shell session when running the script. (just the greeting of the email will change for example email #1 starts off with Dear John and email #2 starts off with Dear Jane)

    Regards,

    Chris

    0

  12. tika 4 months ago

    Hi, I'm trying to send a mail-template file as the body of the message.  How can I do that?

    thanks,

    m:o)

    0

    • Leos Marek 4 months ago

      You cant do that. The Body parameter only accepts string. You can only create a HTML code and put it as the Body with -BodyAsHTML parameter. File can be only added to attachment with -Attachment parameter.

      0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account