Solved

Generate Email Using Exchange Server Account from .bat Script

Posted on 2014-03-24
7
192 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 38

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 38

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

Why are Office 365 signatures so complicated?

Trying to setup transport rules for Office 365 email signatures and can’t quite figure it out? Having to test the signature over and over? Make things simple by using Exclaimer Cloud - Signatures for Office 365.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
In this video we show how to create a Shared Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Sha…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now