Send email via batch file

23527 views
Skip to first unread message

foxidrive

unread,
Jul 13, 2012, 11:12:50 AM7/13/12
to
This sends an email from the command line/batch file, and it's been proven to work in XP and Win7 so far.

Thanks for the idea and underlying code go to a post from a recent thread here, and a newsgroup post from 2007.

Added is the ability to attach a file, and to use command line arguments or set the arguments from within the batch file.




::email-bat.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: defaults
set From=m...@here.com.au
set To=y...@lavabit.com
set Subj="email test %date% %time%"
set Body="did it work? %date% %time%"
set Serv=mail.server.com.au
set Auth=user
set Pass=pass
set fileattach=

:: if command line arguments are supplied then use them
if "%~7" NEQ "" (
set From=%1
set To=%2
set Subj="%~3"
set Body="%~4"
set Serv=%5
set "Auth=%~6"
set "Pass=%~7"
set "fileattach=%~8"
)

call :createVBS "email-bat.vbs"

call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass%
echo email has been sent (if parameters were correct)
pause
del "%vbsfile%" 2>nul
goto :EOF

:send
cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7
goto :EOF

:createVBS
set "vbsfile=%~1"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs = WScript.Arguments
echo >>"%vbsfile%" Set objEmail = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From = objArgs(0)
echo >>"%vbsfile%" objEmail.To = objArgs(1)
echo >>"%vbsfile%" objEmail.Subject = objArgs(2)
echo >>"%vbsfile%" objEmail.Textbody = objArgs(3)
if defined fileattach echo >>"%vbsfile%" objEmail.AddAttachment "%fileattach%"
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusing") = 2 ' not local, smtp
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserver") = objArgs(4)
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = 25
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusername") = objArgs(5)
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendpassword") = objArgs(6)
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = False
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
echo >>"%vbsfile%" .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send






--
Mic

Timo Salmi

unread,
Jul 13, 2012, 12:33:09 PM7/13/12
to
On 13.07.2012 18:12 foxidrive wrote:
> This sends an email from the command line/batch file, and it's been
> proven to work in XP and Win7 so far.

Excellent. And I've put together a few lines on the subject as
http://www.netikka.net/tsneti/info/tscmd188.php

All the best, Timo

--
Prof. (emer.) Timo Salmi, Vaasa, Finland
http://www.netikka.net/tsneti/homepage.php
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.php

Timo Salmi

unread,
Jul 14, 2012, 4:21:38 AM7/14/12
to
On 13.07.2012 19:33 Timo Salmi wrote:
> On 13.07.2012 18:12 foxidrive wrote:
>> This sends an email from the command line/batch file, and it's been
>> proven to work in XP and Win7 so far.

> Excellent. And I've put together a few lines on the subject as
> http://www.netikka.net/tsneti/info/tscmd188.php

If you have taken a look, foxidrive, you will probably have noticed two
essential additions from me to your solution. The error reporting by the
VBScript and the method to take the body of the message from a file. The
rest is just the ordinary polishing and wrap-up.

Timo Salmi

unread,
Jul 14, 2012, 4:26:33 AM7/14/12
to
On 14.07.2012 11:21 Timo Salmi wrote:
> On 13.07.2012 19:33 Timo Salmi wrote:
>> On 13.07.2012 18:12 foxidrive wrote:
>>> This sends an email from the command line/batch file, and it's been
>>> proven to work in XP and Win7 so far.

>> Excellent. And I've put together a few lines on the subject as
>> http://www.netikka.net/tsneti/info/tscmd188.php

> If you have taken a look, foxidrive, you will probably have noticed two
> essential additions from me to your solution. The error reporting by the
> VBScript and the method to take the body of the message from a file. The
> rest is just the ordinary polishing and wrap-up.

And the inclusion of the x-headers.

Bob

unread,
Jul 14, 2012, 11:29:18 AM7/14/12
to
foxidrive typed the following on 7/13/2012 11:12 AM:
> This sends an email from the command line/batch file, and it's been proven to work in XP and Win7 so far.

Greetings foxidrive,

Nice job putting those lines together! It worked perfect also in my
testing using XP SP3 and Win 7 Home Edition.

Best wishes!

foxidrive

unread,
Jul 15, 2012, 4:55:44 PM7/15/12
to
On Sunday 15/07/2012 01:29, Bob wrote:
> foxidrive typed the following on 7/13/2012 11:12 AM:
>> This sends an email from the command line/batch file, and it's been proven to work in XP and Win7 so far.

> Nice job putting those lines together! It worked perfect also in my
> testing using XP SP3 and Win 7 Home Edition.

And Kudos to Timo and his enhancements.

> Best wishes!

And back at'cha!

--
Mic


checkk...@gmail.com

unread,
Aug 10, 2012, 7:18:05 AM8/10/12
to
My attachment size is larger than what can be sent through our exchange server.
How can we zip the attachment before we sent it through the e-mail.

Any help will be very much appreciated.

thanks in advance

Zaidy036

unread,
Aug 10, 2012, 12:45:51 PM8/10/12
to
7Zip is free http://www.7zip.com/

--
Zaidy036

John Gray

unread,
Aug 11, 2012, 4:39:46 AM8/11/12
to
So is right-clicking on the file in Windows Explorer or in My Computer, and choosing "Send to" then "Compressed (zipped) folder".
In fact, this produces a file, not a folder, but Microsoft always seems to have been confused about ZIP(ped) files.

kirahm...@gmail.com

unread,
Dec 18, 2012, 8:33:09 PM12/18/12
to
Thanks!!! your a Great HELP!!
Godbless

Tom Lavedas

unread,
Dec 19, 2012, 10:30:04 AM12/19/12
to
On Friday, July 13, 2012 11:12:50 AM UTC-4, foxidrive wrote:
> This sends an email from the command line/batch file, and it's been
> proven to work in XP and Win7 so far.

I've tried to build a version that eliminates the temporary script file with an approach I developed that uses mshta.exe as the host instead of cscript. I've checked the syntax, but am unable to test the actual email functionality because of my company's internet security settings. It doesn't allow access to external SMTP servers. So, I offer this version with the caveat that it is, as yet, unproven ...

:: email-bat.cmd ::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Modified to eliminate temporary VBS script file
@echo off
setlocal

:: defaults
set From=m...@here.com.au
set To=y...@lavabit.com
set Subj="email test %date% %time%"
set Body="did it work? %date% %time%"
set Serv=mail.server.com.au
set Auth=user
set Pass=pass
set fileattach=

:: if command line arguments are supplied then use them
if "%~7" NEQ "" (
set From=%1
set To=%2
set Subj="%~3"
set Body="%~4"
set Serv=%5
set "Auth=%~6"
set "Pass=%~7"
set "fileattach=%~8"
)

set "VBS=with createobject(""scripting.filesystemobject""):"
set "VBS=%VBS%Execute(.GetStandardStream(0).readALL):"
set "VBS=%VBS%:end with:close"

call :send %1 %2 %3 %4 %5 %6 %7

echo email has been sent (if parameters were correct)
pause
goto :EOF

:send
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
if defined fileattach (
set "FA=echo objEmail.AddAttachment "%fileattach%""
) else (
set "FA="
)
(
echo Set objEmail = CreateObject("CDO.Message"^^^)
echo objEmail.From = "%~1"
echo objEmail.To = "%~2"
echo objEmail.Subject = "%~3"
echo objEmail.Textbody = "%~4"
%FA%
echo with objEmail.Configuration.Fields
echo .Item ("%cdoSchema%/sendusing"^^^) = 2 ' not local, smtp
echo .Item ("%cdoSchema%/smtpserver"^^^) = "%~5"
echo .Item ("%cdoSchema%/smtpserverport"^^^) = 25
echo .Item ("%cdoSchema%/smtpauthenticate"^^^) = 1 ' cdobasic
echo .Item ("%cdoSchema%/sendusername"^^^) = "%~6"
echo .Item ("%cdoSchema%/sendpassword"^^^) = "%~7"
echo .Item ("%cdoSchema%/smtpusessl"^^^) = False
echo .Item ("%cdoSchema%/smtpconnectiontimeout"^^^) = 25
echo .Update
echo end with
echo objEmail.Send
) | mshta.exe vbscript:Execute("%VBS%")

It also lacks the added features that Timo provided.
______________________________
Tom Lavedas

ansh...@gmail.com

unread,
Apr 18, 2013, 4:54:42 AM4/18/13
to

jjbk...@gmail.com

unread,
Aug 7, 2013, 8:22:08 AM8/7/13
to
Could this batch file be modified to send to multiple recipients ?

foxidrive

unread,
Aug 7, 2013, 8:35:32 AM8/7/13
to
On 7/08/2013 22:22, jjbk...@gmail.com wrote:
> Could this batch file be modified to send to multiple recipients ?

Yes, you can use another batch file to call the email send one, with all the details as parameters.

--
foxi

jjbk...@gmail.com

unread,
Aug 7, 2013, 8:49:46 AM8/7/13
to
On Wednesday, August 7, 2013 1:35:32 PM UTC+1, foxidrive wrote:
> On 7/08/2013 22:22, jjbk....@gmail.com wrote:
>
> > Could this batch file be modified to send to multiple recipients ?
>
>
>
> Yes, you can use another batch file to call the email send one, with all the details as parameters.
>
>
>
> --
>
> foxi

Thanks for the prompt reply however I do not fully understand. Can you give me an example or explain further please?
Thanks
jjb

Timo Salmi

unread,
Aug 7, 2013, 12:41:22 PM8/7/13
to
That's different, if I understand your suggestion correctly. What the
recipient(s) will see in the headers will differ. Depending on the
circumstances it may be important to see who else has been on the
recipient list. (Besides, how to send group replies, if the messages are
factually separate.) I do not have a direct answer to the original
question, but I think the answer is yes. Because that's exactly what I
do with the rekindled FAQ pointer posting. Only one posting, one script,
to cover both the "recipient" newsgroups.

foxidrive

unread,
Aug 7, 2013, 10:09:56 PM8/7/13
to
On 8/08/2013 02:41, Timo Salmi wrote:
> On 07.08.2013 15:35 foxidrive wrote:
>> On 7/08/2013 22:22, jjbk...@gmail.com wrote:
>>> Could this batch file be modified to send to multiple recipients ?
>>
>> Yes, you can use another batch file to call the email send one, with
>> all the details as parameters.
>
> That's different, if I understand your suggestion correctly. What the
> recipient(s) will see in the headers will differ. Depending on the
> circumstances it may be important to see who else has been on the
> recipient list. (Besides, how to send group replies, if the messages are
> factually separate.)

That's a good point.

I wonder if the poster has a legit reason to use it, or a spammish reason.


--
foxi

Andy

unread,
Aug 9, 2013, 10:14:15 PM8/9/13
to
> Mic

I don't understand how to use it ?

I see the vbs script that it makes.

Andy

foxidrive

unread,
Aug 9, 2013, 11:10:40 PM8/9/13
to
On 10/08/2013 12:14, Andy wrote:
> On Friday, July 13, 2012 10:12:50 AM UTC-5, foxidrive wrote:
>> This sends an email from the command line/batch file, and it's been proven to work in XP and Win7 so far.

> I don't understand how to use it ?
>
> I see the vbs script that it makes.

This should be on one line.

email.bat m...@gmail.com y...@hotmail.com "This subject is about emails" "This is the body of the email"
smtp.gmail.com username password "c:\folder\file.txt"



To use SSL that gmail requires while sending email, use this line

echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = 465

instead of this:

echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = 25


--
foxi

Bob

unread,
Aug 10, 2013, 10:59:41 AM8/10/13
to
Foxi,
In addition, didn't this line:
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = False

need to be changed to:
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = True

when using gmail?


foxidrive

unread,
Aug 10, 2013, 11:12:00 AM8/10/13
to
I do believe you have spotted my cunning intentional omission. ;) 10 out of 10 for eagle-eye-ness.


Thanks for the correction, Bob.


--
foxi

Andy

unread,
Aug 10, 2013, 9:29:55 PM8/10/13
to
I am getting a vbscript runtime error subscript out of range.

But I am sending from yahoo.com so it may not work.

I don't have a mail server.

I am using wireless internet from an open network, so it may not work ?

Andy

foxidrive

unread,
Aug 11, 2013, 12:05:31 AM8/11/13
to
On 11/08/2013 11:29, Andy wrote:

> I am getting a vbscript runtime error subscript out of range.

Copy and paste the original again, and then modify it again. Take note of Bob's correction.

> But I am sending from yahoo.com so it may not work.

yahoo.com in some locations doesn't allow sending from an email client, but check the above first.

> I don't have a mail server.

The mail server is the one you use to send email from your email client. Your ISP email server for example.

> I am using wireless internet from an open network, so it may not work ?

You mean you are piggy backing off a free network? Get a gmail account and enable pop access and then
try it with that.



--
foxi

Andy

unread,
Aug 11, 2013, 9:32:39 AM8/11/13
to
> foxi

Free internet at my apartment complex.

Thanks, I will try you suggestions.

Andy

Andy

unread,
Aug 11, 2013, 9:59:59 AM8/11/13
to

JJ

unread,
Aug 11, 2013, 10:39:25 AM8/11/13
to
On Sun, 11 Aug 2013 06:59:59 -0700 (PDT), Andy wrote:
> Did everything but still getting same error.
>
> http://classicasp.aspfaq.com/general/why-am-i-getting-subscript-out-of-range-errors.html ???

Make sure you specify parameter #1 to #7 of the batch file.

The VBS error message tells you the exact line number (in the VBS file)
where the error occured. That'll tell you which VBS parameter is missing.

foxidrive

unread,
Aug 11, 2013, 11:01:05 AM8/11/13
to
On 11/08/2013 23:59, Andy wrote:

> Did everything but still getting same error.

Try this batch file - it has been tested with gmail and uses SSL and port 465 to send.

Execute the command as follows, but this command is all on one line.

email.bat bi...@gmail.com s...@gmail.com "This subject is about emails" "This is the body of the email"
smtp.gmail.com bi...@gmail.com password "d:\folder\attachment.txt"


bi...@gmail.com is your email address.
s...@gmail.com is the recipient email address.
"d:\folder\attachment.txt" is the attachment to send.

Re-read Timo's list of batch material as he has some enhancements, and there are other versions of this
after mine, that don't require a temp file.


::email.bat:::::::::::::::::::::::::::::::::::::::::::::::::::::
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = 465
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusername") = objArgs(5)
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendpassword") = objArgs(6)
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = True
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
echo >>"%vbsfile%" .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send
:end



--
foxi

Todd Vargo

unread,
Aug 11, 2013, 1:50:17 PM8/11/13
to
On 8/11/2013 9:59 AM, Andy wrote:
> On Sunday, August 11, 2013 8:32:39 AM UTC-5, Andy wrote:
>> On Saturday, August 10, 2013 11:05:31 PM UTC-5, foxidrive wrote:
>>
>>> On 11/08/2013 11:29, Andy wrote:
>>
>>
>>
>>>
>>
>>>> I am getting a vbscript runtime error subscript out of range.
>>
>>
>>
>>> Copy and paste the original again, and then modify it again. Take note of Bob's correction.
>>
>>>
>>
>>>
>>
>>>
>>
>>>> But I am sending from yahoo.com so it may not work.
>>
>>>
>>
>>>
>>
>>>
>>
>>> yahoo.com in some locations doesn't allow sending from an email client, but check the above first.
>>
>>>
>>
>>>
>>
>>>
>>
>>>> I don't have a mail server.

Then you are using browser based email not a POP account.

>>
>>>
>>
>>>
>>
>>>
>>
>>> The mail server is the one you use to send email from your email client. Your ISP email server for example.
>>
>>>
>>
>>>
>>
>>>
>>
>>>> I am using wireless internet from an open network, so it may not work ?

In other words, you do not have a POP account with yahoo so you are
exactly correct, the batch will not work for you.

>>
>>
>>
>>> You mean you are piggy backing off a free network? Get a gmail account and enable pop access and then
>>
>>>
>>
>>> try it with that.
>>
>>>
>>
>>
>>
>>> foxi
>>
>>
>>
>> Free internet at my apartment complex.

Free internet is irrelevant to the matter at hand. You need a POP account.

>>
>>
>>
>> Thanks, I will try you suggestions.
>>
>>
>>
>> Andy
>
> Did everything but still getting same error.
>
> http://classicasp.aspfaq.com/general/why-am-i-getting-subscript-out-of-range-errors.html ???

What does "did everything" mean? If you don't say it, then we don't know
what exactly was tried, what may have been omitted, or typed/pasted wrong.

On a side note, the added spacing on each reply is terribly annoying.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Andy

unread,
Aug 11, 2013, 3:31:10 PM8/11/13
to
Sorry about the spacing, it's yahoo's fault.

I tried to setup a newsreader but none of the free host sites worked with it.

Andy

Andy

unread,
Aug 11, 2013, 4:00:36 PM8/11/13
to
Which line it ? 18 or 2 or both ?

Andy

C:\MASM32\SOURCE\email-bat.vbs(18, 2) CDO.Message.1: The transport failed to con
nect to the server.

Andy

unread,
Aug 11, 2013, 4:01:56 PM8/11/13
to
On Sunday, August 11, 2013 10:01:05 AM UTC-5, foxidrive wrote:
> On 11/08/2013 23:59, Andy wrote:

Cleaned out by the Todd-o-Cleaner. :-)

> foxi

We are making progress.

Todd Vargo

unread,
Aug 11, 2013, 4:24:10 PM8/11/13
to
Line 18, beginning at the second character.

Andy

unread,
Aug 11, 2013, 5:50:14 PM8/11/13
to
Line 17 end with
Line 18 objEmail.Send

foxidrive

unread,
Aug 11, 2013, 7:52:20 PM8/11/13
to
On 12/08/2013 05:31, Andy wrote:
> Sorry about the spacing, it's yahoo's fault.

It's Google's fault. You're using google groups.
It's a shame that google cant get it right, after so long.

> I tried to setup a newsreader but none of the free host sites worked with it.

Andy, you could do worse than to set up a free account here for 50 MB a day.

http://www.teranews.com

It does require a credit card spend of $4 to set up the account but it never requires you to spend any
more. I've been using the free account forever.



--
foxi

Todd Vargo

unread,
Aug 11, 2013, 11:02:57 PM8/11/13
to
On 8/11/2013 5:50 PM, Andy wrote:
>
> Line 17 end with
> Line 18 objEmail.Send
>

Were you sending it to a POP account? If not, that explains the message.

--

Andy

unread,
Aug 12, 2013, 12:15:38 AM8/12/13
to
Todd,

Thanks for your patience and help.

How would I determine that ?

Andy

I will go with foxi's suggestion.

This group drives me crazy with some of their posters "kiddy ville" bickering.

They have a lot of good information.

alt.comp.freeware

Andy

unread,
Aug 12, 2013, 12:23:15 AM8/12/13