When dealing with legacy Windows Server 2003 R2 systems, sending emails from command line becomes tricky without SMTP services. Many administrators need this capability for automated alerts or monitoring scripts.
For systems where installing third-party tools isn't an option, we can leverage built-in components:
@echo off
set to=peter@example.org
set from=blah@example.org
set subj=Server Alert
set body=Alert! The sky is falling!
blat -to %to% -f %from% -s "%subj%" -body "%body%" -server smtp.example.org
Note: While blat.exe is technically third-party, it's a single executable that doesn't require installation.
For servers with PowerShell 2.0:
$EmailFrom = "blah@example.org"
$EmailTo = "peter@example.org"
$Subject = "Performance Alert"
$Body = "CPU usage exceeded 90% threshold"
$SMTPServer = "smtp.example.org"
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $Subject -Body $Body -SmtpServer $SMTPServer
When PowerShell isn't available, classic VBScript comes to rescue:
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "blah@example.org"
objEmail.To = "peter@example.org"
objEmail.Subject = "Urgent Alert"
objEmail.Textbody = "Server requires immediate attention"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.org"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
For environments allowing outbound SMTP connections:
(
echo HELO yourdomain.com
echo MAIL FROM: <blah@example.org>
echo RCPT TO: <peter@example.org>
echo DATA
echo Subject: Performance Alert
echo From: blah@example.org
echo To: peter@example.org
echo
echo Alert condition detected!
echo .
echo QUIT
) | telnet smtp.example.org 25
1. Most modern SMTP servers require authentication
2. Port 25 might be blocked by default
3. Consider security implications of storing credentials in scripts
Windows Server 2003 R2 administrators often need to trigger email alerts from batch scripts or scheduled tasks. Here are several approaches using only native Windows components:
@echo off
set BLAT_PATH=C:\utils\blat
%BLAT_PATH%\blat.exe -to recipient@domain.com -f sender@domain.com -server smtp.domain.com -subject "Alert" -body "Disk space critical!"
While PowerShell wasn't native in 2003 R2, many admins installed it later. Here's a VBS alternative:
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "admin@yourdomain.com"
objEmail.To = "recipient@domain.com"
objEmail.Subject = "Server Alert"
objEmail.Textbody = "CPU usage exceeded threshold"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.yourdomain.com"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
For quick testing when you have SMTP credentials:
telnet smtp.server.com 25
HELO yourdomain.com
MAIL FROM: <sender@domain.com>
RCPT TO: <recipient@domain.com>
DATA
Subject: Alert Message
This is the message body.
.
QUIT
Save this as sendmail.js:
var objMessage = new ActiveXObject("CDO.Message");
objMessage.Subject = "Server Alert";
objMessage.From = "admin@domain.com";
objMessage.To = "admin@domain.com";
objMessage.TextBody = "Performance threshold exceeded";
objMessage.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
objMessage.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.domain.com";
objMessage.Configuration.Fields.Item(
"http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25;
objMessage.Configuration.Fields.Update();
objMessage.Send();
WScript.Echo("Message sent");
Call it from command line with: cscript sendmail.js
- Never store credentials in plain text scripts
- Consider SMTP relay restrictions on your network
- Test with internal mail servers before production use
- Some methods require CDOSYS component (installed by default on 2003 R2)