EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Send Mail
Secondary Sidebar
PowerShell Tutorial
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Send Mail

By Priya PedamkarPriya Pedamkar

PowerShell Send Mail

Introduction to PowerShell Send Mail

To send an email message from PowerShell the Send-MailMessage Cmdlet is used. Many times, the administrator may require a script to run to generate some reports like when the last reboot time was, how many times the server was down in the last 24 hours, etc. In this scenario, an email with the report must be sent to the administrator. For this purpose, Send-MailMessage cmdlet is used. The main parameter of this cmdlet is the SMTP server. There must be a valid SMTP server configured in the environment to be able to send and receive emails. In case if there is no SMTP server mentioned, PowerShell will take the variable set in the $PSEmailServer variable.

Syntax and Parameters

Below we will see the syntax and parameters

Syntax

Below is the syntax

Code:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

NAME
Send-MailMessage
SYNTAX
Send-MailMessage [-To] <string[]> [-Subject] <string> [[-Body] <string>] [[-SmtpServer] <string>] -From <string> [-Attachments <string[]>] [-Bcc <string[]>] [-BodyAsHtml] [-Encoding <Encoding>] [-Cc <string[]>] [-DeliveryNotificationOption {None | OnSuccess | OnFailure | Delay | Never}] [-Priority {Normal | Low | High}] [-Credential <pscredential>] [-UseSsl] [-Port <int>]  [<CommonParameters>] ALIASES

Parameters

Below are the parameters:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,527 ratings)
  • Attachments: This parameter denotes the files that need to be added as attachments in the email to be sent. Its type is a string. Its alias is PSPath. The default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • Bcc: This denotes the email addresses of the recipients who will get the email but won’t be marked directly. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • Body: This denotes the main body of the email. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • BodyAsHtml: This denotes the body of the message to be converted as HTML. Its type is the switch parameter. Its alias is BAH. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • CC: It denotes the recipients who should receive the carbon copy of this message. Its type is a string. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • Credential: This denotes the credentials that should be used for the send-mailmessage cmdlet. By default, current user credentials are taken. Its type is PSCredential. Its default value is the current user. It accepts pipeline input but doesn’t accept wildcard characters.
  • DeliveryNotificationOption: This denotes the notification status of the email sent. It can be multiple values. Its alias is DNO. Its default value is none. The address specified will receive the notification. It accepts pipeline input but doesn’t accept wildcard characters. The allowed values are
  • None: This means no notification is sent.
  • OnSuccess: This denotes a notification is sent if the mail is delivered successfully.
  • Onfailure: This sends a notification if the mail is not delivered.
  • Delay: This sends a notification if there is a delay in sending the email.
  • Never: This denotes any notification.

Encoding: This denotes the encoding type. The acceptable values are as follows:

  • ASCII
  • OEM
  • Unicode
  • UTF7
  • UTF8
  • UTF8BOM
  • UTF8NoBOM
  • UTF32

Its type is encoding, and the alias is BE. The default value is UTF8NoBOM.It accepts pipeline input but doesn’t accept wildcard characters.

  • From: This is a mandatory parameter and denotes the address that should be in the email. This denotes the sender. Its type is a string. Its default value is the current user. It accepts pipeline input but doesn’t accept wildcard characters.
  • Port: Denotes the port number of the SMTP Server. By default, it’s 25. Its type is Int32. It accepts pipeline input but doesn’t accept wildcard characters.
  • Priority: This denotes the priority of the email. The default value is normal. The other acceptable values are high and low. Its type is MailPriority.It accepts pipeline input but doesn’t accept wildcard characters.
  • ReplyTo: This denotes the email addresses to which the receiver can reply other than the from address. Its type is a string and default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • SMTPServer: This denotes the SMTP Server. The default value is the value present in the $PSEmailServer environment variable. Its type is string and alias is ComputerName.It accepts pipeline input but doesn’t accept wildcard characters.
  • Subject: This denotes the subject of the email. This is a mandatory parameter. Its type is a string. Its alias is sub. The default value is none. It accepts pipeline input but doesn’t accept wildcard characters.
  • To: This denotes the recipient’s address. This is a mandatory parameter. Multiple addresses are allowed, and they are separated by “;”. Its type is a string and default value is none. It accepts pipeline input but doesn’t accept wildcard characters. It accepts pipeline input but doesn’t accept wildcard characters.
  • UseSsl: This is used for the secure transmission of email. Its type is the switch parameter. Its default value is none. It accepts pipeline input but doesn’t accept wildcard characters.

Example to Implement PowerShell Send Mail

In the below script, kindly modify the from address($from), to address($to) and smtp server($SmtpServer) and attachment path($testattachment) with your appropriate values.

Code:

Write-Host "Welcome to the demo of sending email in PowerShell"
$from="test@test.com"
Write-Host "From address will be" $from
$to="************"
$subject="This is an example of test mail from PowerShell"
Write-Host "Subject of the email is" $subject
$body="This is a test email. This is sent from powershell using Send-MailMessage Cmdlet"
Write-Host "The body of the email will be" $body
$SmtpServer = "***********"
$html=""
$actualbody = "<html>
<style>
BODY{font-family: Arial; font-size: 12pt;}
H1{font-size: 26px;}
H2{font-size: 24px;}
H3{font-size: 08px;}
</style>
<body>
<h1 align=""center"">Test Email</h1>
<h3 align=""center"">$body</h3>
<p>Welcome</p>
<p>Test email</p><br/>" + $html
$testattachment="C:\Users \Desktop\Articles\Mar\PowerShell Wild cards.docx"
Send-MailMessage -From $from -to $to -Subject $subject -SmtpServer $SmtpServer -Body $actualbody -BodyAsHtml -Encoding ([System.Text.Encoding]::UTF8)
Write-Host "Email Sent"

Output:

PowerShell Send Mail - 1

Below is how the received email is looked like.

PowerShell Send Mail - 2

Conclusion

Thus, the article covered in detail about how an email is sent from PowerShell with the help of Send-MailMessage Cmdlet. It explained the different parameters that are required, their type and showed an example script of how to send an email. The most important parameter that is required is the SMTP server unless an SMTP server is configured email won’t be sent. The way to learn more about the cmdlet would be explored by writing sample scripts.

Recommended Articles

This is a guide to Powershell Send Mail. Here we discuss an introduction to Powershell Send Mail along with syntax and parameters, with examples. You can also go through our other related articles to learn more –

  1. PowerShell Get-Item
  2. PowerShell Grep
  3. PowerShell Convert to String
  4. PowerShell Alias
  5. Guide to PowerShell Get-Service
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more