Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Email via VBScript

17 views
Skip to first unread message

Si Ballenger

unread,
Jul 14, 2007, 12:00:50 PM7/14/07
to
The below batch file using vbscript can be used to send simple
email messages on xp machines. The below link has more info on
this type of setup with more possible options. I use it in an
unattended monitoring program that notifies me of changing
inputs.

http://www.paulsadowski.com/WSH/cdo.htm

Lines below placed two spaces to the right to identify word wrap.


@echo off
echo Set objArgs = WScript.Arguments > bat-email.vbs
echo from = objArgs(0) >> bat-email.vbs
echo sentto = objArgs(1) >> bat-email.vbs
echo subject = objArgs(2) >> bat-email.vbs
echo body = objArgs(3) >> bat-email.vbs
echo server = objArgs(4) >> bat-email.vbs
echo Set objEmail = CreateObject("CDO.Message") >>
bat-email.vbs
echo objEmail.From = from >> bat-email.vbs
echo objEmail.To = sentto >> bat-email.vbs
echo objEmail.Subject = subject >> bat-email.vbs
echo objEmail.Textbody = body >> bat-email.vbs
echo objEmail.Configuration.Fields.Item _ >> bat-email.vbs
echo
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
>> bat-email.vbs
echo objEmail.Configuration.Fields.Item _ >> bat-email.vbs
echo
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
>> bat-email.vbs
echo server >> bat-email.vbs
echo objEmail.Configuration.Fields.Item _ >> bat-email.vbs
echo
("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
= 25 >> bat-email.vbs
echo objEmail.Configuration.Fields.Update >> bat-email.vbs
echo objEmail.Send >> bat-email.vbs
::the below should be on one line with appropriate info filled
in
start cscript.exe bat-email.vbs m...@home.net y...@home.net "email
test" "did it work?" smtp.yourisp.net

Matthias Tacke

unread,
Jul 14, 2007, 6:06:16 PM7/14/07
to
Si Ballenger wrote:
> The below batch file using vbscript can be used to send simple
> email messages on xp machines. The below link has more info on
> this type of setup with more possible options. I use it in an
> unattended monitoring program that notifies me of changing
> inputs.
>
> http://www.paulsadowski.com/WSH/cdo.htm
>
> Lines below placed two spaces to the right to identify word wrap.

Hi Si,
I don't like that type of wrapping vbs in a batch, so I rewrote/enhanced
your version to pass cmd line arguments from cmd to vbs with defaults
being overwritten if all needed arguments are present.
BTW nice trick IMO ;-)

I also added user/pass for authentication at the smtp server
(which I need here)
Using:
with objEmail.Configuration.Fields
And stuffing
http://schemas.microsoft.com/cdo/configuration
into a variable makes the code easier to read (IMO).
The file attachment in the vbs file is commented out, to enable
simply remove the '

::bat-email.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off&setlocal
set vbsfile=bat-email.vbs
if not exist "%vbsfile%" call :createVBS echo ^^>^^>%vbsfile%
:: defaults
set From=m...@home.net
set To=y...@home.net
set Subj="email test %date% %time%"
set Body="did it work? %date% %time%"
set Serv=smtp.yourisp.com
set Auth=YourAuth
set Pass=YourPassw
:: if all args on cmdline, overwrite defaults
if %7. NEQ . for %%A in (From To Subj Body Serv Auth Pass
) do call set %%A=%%1& shift


::the below should be on one line with appropriate info filled in

cscript.exe %vbsfile% %From% %To% %Subj% %Body% %Serv% %Auth% %Pass%
goto :eof
:createVBS
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
%* Set objArgs = WScript.Arguments
%* Set objEmail = CreateObject("CDO.Message")
%* objEmail.From = objArgs(0)
%* objEmail.To = objArgs(1)
%* objEmail.Subject = objArgs(2)
%* objEmail.Textbody = objArgs(3)
%* ' objEmail.AddAttachment "%~f0"
%* with objEmail.Configuration.Fields
%* .Item ("%cdoSchema%/sendusing") = 2 ' not local, smtp
%* .Item ("%cdoSchema%/smtpserver") = objArgs(4)
%* .Item ("%cdoSchema%/smtpserverport") = 25
%* .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
%* .Item ("%cdoSchema%/sendusername") = objArgs(5)
%* .Item ("%cdoSchema%/sendpassword") = objArgs(6)
%* .Item ("%cdoSchema%/smtpusessl") = False
%* .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
%* .Update
%* end with
%* objEmail.Send
::bat-email.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::

--
Greetings
Matthias

Pegasus (MVP)

unread,
Jul 15, 2007, 4:14:45 AM7/15/07
to

"Matthias Tacke" <Matt...@Tacke.de> wrote in message
news:f7bodo...@news.tacke.de...

I'm a little confused about this thread. Here is why.

- If I wanted a batch file solution to send an EMail note then
I would use a ready made tool such as blat.exe. Nice and
easy to use, very powerful, far simpler that the OP's solution:
blat.exe -to Receiver -subject Test -f sender Sender -server SMTP

- If I wanted a script solution then I would look in a scripting
newsgroup rather than wrapping a script into a batch file which
will ultimately generate a script file anyway.

Putting it in a different way: Did the OP go to the trouble of
wrapping his script into a batch file only in order to be able
to post it here in this batch newsgroup?


Message has been deleted

Matthias Tacke

unread,
Jul 15, 2007, 7:06:45 AM7/15/07
to
Guy wrote:

> Pegasus (MVP) wrote:
>
>> I'm a little confused about this thread. Here is why.
>>
>> - If I wanted a batch file solution to send an EMail note then
>> I would use a ready made tool such as blat.exe. Nice and
>> easy to use, very powerful, far simpler that the OP's solution:
>> blat.exe -to Receiver -subject Test -f sender Sender -server SMTP
>
> In some enviroments use of third party tools is prohibited.

Ack
The portability of a combined batch/script as a single file is IMO
much better. And cmd/WSH are present on almost all windows pcs.

>> - If I wanted a script solution then I would look in a scripting
>> newsgroup rather than wrapping a script into a batch file which
>> will ultimately generate a script file anyway.
>

> Are not "scripts" and "batch files" the same thing?
> After all UNIX "batch files" are shell scripts.
>
Its fun for me to combine the strength of them;
what you can do with a single for /f will need several lines of code in
vbscript.

>> Putting it in a different way: Did the OP go to the trouble of
>> wrapping his script into a batch file only in order to be able
>> to post it here in this batch newsgroup?
>>

If coded manually - it might be trouble, but I use an editor with regex
capabilities and the way I did it leaves the code quite readable ;-)
>
> Probably he had the task or the idea to perform the job with native
> toolset. Once arrived at a solution he shared with public.
>
For scheduling/remoting reasons batches are often used to run scripts,
so why not have a single source?

--
Greetings
Matthias

Si Ballenger

unread,
Jul 17, 2007, 12:18:30 AM7/17/07
to
On Sun, 15 Jul 2007 10:14:45 +0200, "Pegasus \(MVP\)"
<I....@fly.com> wrote:


>I'm a little confused about this thread. Here is why.
>
>- If I wanted a batch file solution to send an EMail note then
> I would use a ready made tool such as blat.exe. Nice and
> easy to use, very powerful, far simpler that the OP's solution:
> blat.exe -to Receiver -subject Test -f sender Sender -server SMTP

For various reasons, some like to use native windows solutions
instead of third party executables. I like making something I
find useful just using notepad.

>- If I wanted a script solution then I would look in a scripting
> newsgroup rather than wrapping a script into a batch file which
> will ultimately generate a script file anyway.

Not all persons follow all scripting groups. I posted it for
those that follow this group that might find it useful.

>Putting it in a different way: Did the OP go to the trouble of
>wrapping his script into a batch file only in order to be able
>to post it here in this batch newsgroup?

See the above.

Si Ballenger

unread,
Jul 17, 2007, 12:23:26 AM7/17/07
to
On Sun, 15 Jul 2007 00:06:16 +0200, Matthias Tacke
<Matt...@Tacke.de> wrote:

>Hi Si,
>I don't like that type of wrapping vbs in a batch, so I rewrote/enhanced
>your version to pass cmd line arguments from cmd to vbs with defaults
>being overwritten if all needed arguments are present.
>BTW nice trick IMO ;-)
>
>I also added user/pass for authentication at the smtp server
>(which I need here)
>Using:
> with objEmail.Configuration.Fields
>And stuffing
> http://schemas.microsoft.com/cdo/configuration
>into a variable makes the code easier to read (IMO).
>The file attachment in the vbs file is commented out, to enable
>simply remove the '

Nice cleanup of the code Matthias. I don't have much luck with
vbs files, so when I found one that worked, I left it as is
instead of attempting to tweek it.


Message has been deleted

Norman Bullen

unread,
Jul 17, 2007, 10:12:05 PM7/17/07
to
Matt Williamson wrote:
>
< snipped >
> Hybrid scripts have their place. Take this script I wrote awhile back.
> Without it, I'd have to use Set /P and type the full path to a folder. When
> you start getting into multiple levels of long paths, it's a PITA. I already
> had the script I integrated it into fully written, tested and working but I
> wanted to make it a little easier to use and this allows me to do it, which
> was impossible in pure CMD script (outside of using ASM or an external
> program). The way I see it, it just adds another tool to the arsenal and the
> more tools you have, the easier a task becomes.
>
> :BrowseFolder
> set Result=
> set input=%1
>
> :: Temporary files
> set vbs=%temp%\_.vbs
> set tmp=%temp%\_.cmd
>
>
< snipped >
>
> DEL %VBS%
> DEL %TMP%

As a general rule, you should be careful of the %TEMP% variable. It may
contain a path to a directory in the user's Documents and Settings
directory which, obviously, contains spaces.

If your script must be portable to other computers, always surround it
with quotation marks whenever you build your own path using %TEMP%.

Norm

Si Ballenger

unread,
Jul 21, 2007, 11:09:04 AM7/21/07
to
On Sun, 15 Jul 2007 00:06:16 +0200, Matthias Tacke
<Matt...@Tacke.de> wrote:

Just for those that might have a need, I've found that I can use
this to send text messages to my cell phone. In my setup, if
certain conditions are met, it sends me a text message
notification. Might be handy for those on the go.

0 new messages