Solved

Generate Email Using Exchange Server Account from .bat Script

Posted on 2014-03-24
7
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
7 Comments
 
LVL 41

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 41

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

688 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