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
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
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?
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
>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.
>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.
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
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.