In this article I will explain you the configuration and script required to send SMTP Email. I configured it on Windows Server 2008 R2 with IIS 6.0.
Part 1 – Configure SMTP Server on the machine
Make sure Internet Information Services (IIS) is installed on your server. Next install the SMTP Server.
On your server, open Server Manager, right click on Features, click on the Add Features. In the Add Features Wizard, select the checkbox for SMTP server and install it
Note – On my server it was already configured.
Open the IIS 6.0 Manager, open the properties for your SMTP Virtual Server.
Click on the Relay button
Add the IP address [127.0.0.1] for the localhost and click OK.
Open the IIS Manager and double click on the SMTP E-mail.
Make sure the port no 25 is configured to send email for the your SMTP server (localhost).
Open Windows Firewall with Adanvced Security and check the inbound rule for SMTP is enabled and showing port 25. If it is not there then created the rule.
Part 2 – Scripts for sending E-mail
To send email, I kept all my files (content.htm and sendmymail.ps1) in a folder c:\email
- Content.htm
I wrote an HTML file called content.htm which has all the content text I need to send through SMTP email. The text was just 🙂
Hi Buddy,
How are you doing? Come join the party this weekend.
Cheers,
Vijay
- sendmymail.ps1
The script for for Powershell have .ps1 extention. Note that on my server powershell version 2.0 is configured. This file was created in notepad. The content of the file c:\email\contentSuccess.htm is as below:
###########Define Variables########
$fromaddress = “donotreply@sambhe.ie”
$toaddress = “abcdefg123455@gmail.com”
$Subject = “Hello There”
$body = get-content c:\email\content.htm
$smtpserver = “localhost”
####################################
$message = new-object System.Net.Mail.MailMessage
$message.From = $fromaddress
$message.To.Add($toaddress)
$message.IsBodyHtml = $True
$message.Subject = $Subject
$message.body = $body
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$smtp.Send($message)
$error[0]|format-list -force
################################################################
The syntax is understandable. The $body is pointing to the htm file. $smtpserver is pointing to localhost, From and To addressed are given, subject is entered.
- Powershell syntax
Excecute the following command in the Command window.
Powershell.exe -executionpolicy remotesigned -File c:\email\sendmymail.ps1
The script above will send the email to abcdefg123455@gmail.com. This can be put in a batch file and configure as a scheduled task. You can make your processes automated e.g. run a process and send email to IT manager/Admin about the status of the execution etc.
If you already have a SMTP server in your network and you want to use it to send the email then in the .ps1 script $smtpserver should point to your standard SMTP server by its identifier (ip or host name). On your SMTP server though you need to add the IP address of this server in the Relay setting (shown above).
Excellent and useful post.
LikeLike
Saved from the nightmare I had for the last few days.
LikeLike