Send Email via batch file

Posted on 2010-09-20
Medium Priority
Last Modified: 2012-05-10
I am trying to create a batch file that will send an email to myself. I want to be alerted when services on Windows servers stop.
This will be used on Windows Server 2003 with Exchange 2003. The code I am using is below:

@echo off & setlocal
:: set the temp file location
set tempmail=%temp%\tempmail.%random%.txt
:: echo the basic headers to the temp file
echo To: "Scripting Test" ^<me@mydomain.com.au^> > %tempmail%
echo From: "Admin" ^<them@theirdomain.com.au^> >> %tempmail%
echo Subject: Test2 >> %tempmail%
:: echo the blank line that separates the header from the body text
:: echo the body text to the temp file
echo First line of body text.>> %tempmail%
echo Second line of body text.>> %tempmail%
:: move the temp file to the mail pickup directory
:: adjust this location for your system
move %tempmail% c:\inetpub\mailroot\pickup
set tempmail=

The file gets created in the c:\inetpub\mailroot\pickup directory but then nothing happens. There is also a pickup directory under the exchange folders but that didn't work either.
Can anyone help?

Question by:nealerocks
LVL 14

Accepted Solution

Erik Pitti earned 1800 total points
ID: 33722396
Try using a copy, then delete the original file instead of a move.  A copy operation updates the NTFS filesystem differently than a move if the source and destination directories are on the same drive letter.

copy %tempmail% c:\inetpub\mailroot\pickup
del %tempmail%

Also you should be using the pickup folder under mailroot in exchange

Alternatively, you can use the BLAT.exe smtp client to send mail instead of writing directly to the pickup folder.


LVL 14

Expert Comment

by:Shabarinath Ramadasan
ID: 33723049
Yea.. Agree with Chakote..
Blat is more flexible.
The whole code what you did can be achieved using a one liner.

Good luck.
LVL 12

Author Comment

ID: 33723185
When I change the script to use C:\program files\exchsrvr\vsi 1\pickup i get the error:

"the syntax is wrong"
LVL 31

Assisted Solution

MegaNuk3 earned 200 total points
ID: 33723850
try putting " around the long path name like "C:\program files\exchsrvr\vsi 1\pickup"

use the short name like c:\progra~1\exchsrvr\vs~1 ( you'll have to check that is correct, I am not in front of an Exchange server to test that at the moment)
LVL 31

Expert Comment

ID: 33732971
Thanks for the points, glad you got it working.

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This article describes Top 9 Exchange troubleshooting utilities that every Exchange Administrator should know. Most of the utilities are available free of cost. List of tools that I am going to explain in this article are:   Microsoft Remote Con…
If there is anything erroneous with Exchange Database, it causes a significant effect on email communication till the user remounts the database. Further, database crash directly affects Outlook users due to which they are unable to access their ema…
This video discusses moving either the default database or any database to a new volume.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question