Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Generate Email Using Exchange Server Account from .bat Script

Posted on 2014-03-24
7
Medium Priority
?
220 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 43

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 43

Accepted Solution

by:
Adam Brown earned 1000 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

New style of hardware planning for Microsoft Exchange server.
As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Introduction to Processes
Suggested Courses

604 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