Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call Microsoft oulook from a Batch File 1

Status
Not open for further replies.

MAQAQM

IS-IT--Management
Jun 9, 2005
32
FR
Dear Gurus,

I would like some help. I need a code of command line(.bat) which calls Microsoft outlook and automatically attaches file from the C:\drive to send the email automatically also.

please help on that. if i just have a code of batch file which calls outlook and attaches file, I would be very greatfull.

thanks
Augusto
 
Take a look at a program called blat. It might be more suited to this task.

The answer is "42"
 
This can be done with a simple vbscript if you have an SMTP relay.
Code:
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "attachment@domain.com"
objEmail.To = "me@domain.com"
objEmail.Subject = "Subject of e-mail"
objEmail.AddAttachment "c:\folder\attachthisfile.txt"
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "smtprelay.com"
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send
 
how can I use this code and when to place it:

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "attachment@domain.com"
objEmail.To = "me@domain.com"
objEmail.Subject = "Subject of e-mail"
objEmail.AddAttachment "c:\folder\attachthisfile.txt"
objEmail.Configuration.Fields.Item _
(" = 2
objEmail.Configuration.Fields.Item _
(" = _
"smtprelay.com"
objEmail.Configuration.Fields.Item _
(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send

please explain. in exchange or batch file?

AQM
 
That code is actually a vbscript. So you want to copy it to a text document and save it with a .vbs extension, and of course, changing the e-mail addresses, subject, smtp server, etc. You would then call it from a batch file like so:

cscript mailscript.vbs

It will attach whatever file you specify on the 5th line. This script assumes that the name of the file you want to send doesn't change. If it does, the script will have to be modified to accept input and pass that to the "addattachment" line. Let me know if you need to do that or if this will work for you.
 
I guess I should clarify - you need to plug in the address of your exchange server in the script where it says:

smtprelay.com

To see if your exchange server will act as a relay, you can test by typing the following from a command prompt:

telnet your-exchange-ip 25

It will either give you an error or connect. Type quit to exit the telnet session. If it gives you an error, this won't work for you unless you have another smtp relay upstream from your exchange server that you can use.
 
Yes, I want to be adding the sysdate to the script after attachments or attach them with different names.
 
So basically, you want an e-mail that looks like this:

From: mysystem@domain.com
To: me@domain.com
Subject: "today's date" blah report
Attachment: differentfile.doc

Have you verified yet if your server will do smtp relay by doing the telnet thing yet?
 
Yes dear, thanks.
I did the telnet and it worked. it connected to the exchange server. please give next instructions.

thanks
AQM
 
The date is easy to add, but I need more information to help you with the changing files. There has to be a way for you to identify in your batch file (or in the vbscript) which file you're going to send. Is it a file in a certain directory? Is it always going to be the only file with today's date?

To include the date, just put "& date" on the subject line as shown below in red.

Code:
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "attachment@domain.com"
objEmail.To = "me@domain.com"
objEmail.Subject = "Report for "[COLOR=red] & date[/color]
objEmail.AddAttachment "c:\folder\attachthisfile.txt"
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "smtprelay.com"
objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send
 
I will be basically doing the following:

- The batch file export the data into a .txt
- and then this same batch file at the end of the export, it calls the VB script you gave me to send the generated files to the client immediatelly.

- The files I will be sending are in the same folder(d:\pdms\pdms_import_export\export\to_send\Daily_sonangol_pdms.txt and another d:\pdms\pdms_import_export\export\to_send\Daily_test_sonangol.txt), but in this same folder to_send, that's where the export will be dumping the .txt files above for each day.
the file will have the same name but with different export data from the database which is generated from offshore on a daily basis.
- so finally, after all this is done, I am sure I have the e-mail sent if it works of course.

please I really need this, help
AQM
 
Well, you should be able to create this vbscript file, place it in the same directory with your batch file, and call it at the end of your script that does the actual export with a line like this:

cscript c:\folderpath\whatevername.vbs

This should work for you as-is. Just customize the e-mail addresses and paths to the attachment files. To add those attachments, you'd just add these lines in place of my "addattachment" line:

objEmail.AddAttachment "d:\pdms\pdms_import_export\export\to_send\Daily_sonangol_pdms.txt"
objEmail.AddAttachment "d:\pdms\pdms_import_export\export\to_send\Daily_test_sonangol.txt"
 
thanks man. it really worked. i Tested it and it is great!!
 
Dear guru,

it just one problem. it works for the internall e-mail only. when I send it out of my network it gives the message: unable to deliver to amaquengo45@hotmail.com. and I am to add Bcc and Cc recipients but does not accept. is there any other trick?

AQM
 
This will probably be a question for your Exchange admin. It's possible they have restricted mail relay to internal addresses only.

But, just to be sure, have you tried test messages to other addresses just to make sure it's not a problem with that particular address?
 
I tried several different address not still it does not work. it says: unable to relay to th email address. I spoke to the exchange admin, and says that there is no restriction, otherwise they would not be able to send e-mails.

thanks
AQm
 
Try this:

Telnet to your mail server again on port 25 and follow the instructions to manually send a message to an external address, using yourself as the return sender.


This will isolate the problem to the server or the script itself. I find it unlikely that it's the script because you can send to internal addresses fine. There are restrictions your admins can put on Exchange that will cause these exact symptoms, so I wouldn't just take their word that everything is wide-open.
 
Actually, here is probably your answer. The default behavior for Exchange 2000/2003 is to not allow "unauthenticated" users to relay:


I'm not sure if there's a way for you to authenticate from within the script. If not, you'll have to ask them to remove this restriction. They may be reluctant to do so, but if their firewall and/or ACLs are set up correctly, it's not much of an issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top