Solved

Generate Email Using Exchange Server Account from .bat Script

Posted on 2014-03-24
7
198 Views
Last Modified: 2015-05-01
I am developing a process by which an employee in my accounting department can run a script that will edit a text file and then send an email. We use exchange server and the email is running on server03 while the batch script will actually be on server04. I have spent most of the day going through previous posts and Google to look for a solution. I see a number of suggestions for blat and sendmail but all seem to be relating to a web hosted SMTP email with nothing directly mentioning how to pull this off in a corporate environment using exchange.

I want to generate an email in the .bat file that will generate an email with the following parameters:

To: sparker@myemailaddy.com
From: administrator@myemailaddy.com
Subject: File AUTOPOST.INI has been Edited
Body: S-FSIFL has been added to AUTOPOST.INI file.

And that's it. Sounds so simple but I have spent a lot of time trying to find examples that just do not seem to fit.

Thanks in advance!
0
Comment
Question by:sparker1970
  • 2
  • 2
7 Comments
 
LVL 39

Expert Comment

by:Adam Brown
ID: 39951465
With a batch file, you would need to have some third party tool that can send email from the command line. You may want to look into building your script with powershell 2.0 or 3.0 instead. Powershell 2.0 came with the send-mailmessage cmdlet that allows you to send email messages against a set server without having a third party utility.
0
 

Author Comment

by:sparker1970
ID: 39951475
I do have PowerShell as an option if you have some insight you can offer on how to build it.
0
 
LVL 39

Accepted Solution

by:
Adam Brown earned 250 total points
ID: 39951616
http://technet.microsoft.com/en-us/library/hh849925.aspx has full instructions on using send-mailmessage. You would do something like this:

$to = "sparker@myemailaddy.com"
$from = "administrator@myemailaddy.com"
$subject = "File AUTOPOST.INI has been Edited"
$body = "S-FSIFL has been added to AUTOPOST.INI file."
$server = <whatever your exchange server address is>
send-mailmessage -to $to -subject $subject -body $body -from $from -server $server

Open in new window


That would send the message using anonymous relay, so you would need to set up a receive connector on the Exchange server that allows anonymous relaying. http://technet.microsoft.com/en-us/library/bb232021%28v=exchg.141%29.aspx has info on how to do this. You would want to limit that connector so it will only accept anonymous relaying from server4 in your environment. Don't know what the rest of your script needs to do, but that will take care of the email portion of it.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 39951886
Well as long as you have SMTP server available and able to relay to where you want it to go then just point either BLAT or a VBScript to it with the details you want.  This script of mine shows you both ways:

http://scripts.dragon-it.co.uk/links/email-from-batch
http://scripts.dragon-it.co.uk/links/batch-monitor-ping-email

etc.

REM Your batch file, 
notepad "c:\somedir\myfile-to-edit.txt"
cscript //nologo "c:\somedir\sendmail.vbs"

Open in new window



e.g. this is sendmail.vbs

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Const MailServer = "x.x.x.x" ' Mail Server to use for SMTP
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Const MailTo = "sparker@myemailaddy.com" ' Who should be notified
Const MailFrom = "administrator@myemailaddy.com"
subject = "File AUTOPOST.INI has been Edited"
Body ="S-FSIFL has been added to AUTOPOST.INI file."

on error resume next

REM Get body of message if you want to from a text file instead of in the VBS:
REM Remove these lines if not wanted, or remove the REM to make them work
REM Dim fso, f
REM Set fso = CreateObject("Scripting.FileSystemObject")
REM Set f = fso.OpenTextFile(""c:\somedir\myfile-to-edit.txt", ForReading)
REM Body = f.ReadAll
REM f.Close
REM Set f = Nothing
REM Set fso = Nothing

SendMail MailFrom , MailTo , subject , body, MailServer , MailServerPort

Sub SendMail(Sender, Recipient, Subject, Message, Server, Port)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = Sender
objEmail.To = Recipient
objEmail.Subject = Subject
objEmail.Textbody = Message
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Server
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
objEmail.Configuration.Fields.Update()
objEmail.Send
End Sub

Open in new window




Steve
0
 

Author Comment

by:sparker1970
ID: 39955819
Thanks for the responses...I will be testing some of these today and will post an update as to how these worked.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

856 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