Followers

Simple Powershell Script to Send Email on Windows Server 2012

Sending an email from Server 2012 using Powershell is a VERY simple process.
Below is a very simple example of sending an email using a powershell script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
$message = @"                          
Hello,
I have bad news.  It's not tragic, but you will have to do a small amount of work.
You only have 10% of space left on your hard drive.
You need to do some disk cleanup!!
Thank you,
Your Faithful Web Serving Machine
"@      
$smtpserver="smtp-server-name"
$emailTo = "sample@example.com"
$emailFrom = "administrator@example.com"
$subject="WARNING | WWW | Low Disk Space"
$msg = new-object Net.Mail.MailMessage
$msg.To.Add($emailTo)
$msg.From = $emailFrom
$msg.Subject = $subject
$msg.Body = $message
$smtp=new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($msg)

No comments:

Post a Comment