Solved

Generate Email Using Exchange Server Account from .bat Script

Posted on 2014-03-24
7
189 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this video we show how to create a mailbox database 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 Servers >> Data…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

18 Experts available now in Live!

Get 1:1 Help Now