Emailing with batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Notelek_Labs
Posts: 16
Joined: 22 Jul 2012 20:26

Re: Emailing with batch

#16 Post by Notelek_Labs » 23 Jul 2012 13:08

not really, just refering to your username

EDIT: Oh god, just googled it. Didn't mean that, its just the first four letters of your username. sorry if that was offensive

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Emailing with batch

#17 Post by Liviu » 23 Jul 2012 13:23

Notelek_Labs wrote:Ok first off, read my first post.

No, first off, you please read the replies ;-) It's been pointed, linked and repointed that gmail requires port 465 with SSL authentication. Each of the two keywords shows up exactly once in the code - change smtpserverport from 25 to 465, and smtpusessl False to True. It will work.

Liviu

Notelek_Labs
Posts: 16
Joined: 22 Jul 2012 20:26

Re: Emailing with batch

#18 Post by Notelek_Labs » 23 Jul 2012 14:12

Current code:

Code: Select all

@echo off
setlocal

:: defaults
set From=noteleklabssecuredevices@gmail.com
set To=**********@txt.att.net
set Subj="email test   %date% %time%"
set Body="did it work? %date% %time%"
set Serv=smtp.gmail.com:465
set Auth=noteleklabssecuredevices@gmail.com
set 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%
pause
del "%vbsfile%" 2>nul
goto :EOF

:send
cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7 >nul 2>nul
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")       = True
echo >>"%vbsfile%"  .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send
rem

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Emailing with batch

#19 Post by Liviu » 23 Jul 2012 14:22

No, that's still not right. Remove ":465" from the "Serv=" line. Do this, instead.
Liviu wrote:change smtpserverport from 25 to 465

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Emailing with batch

#20 Post by foxidrive » 23 Jul 2012 16:33

It works for me using the code as I posted and this format - my gmail account is also enabled for pop/smtp

Code: Select all

:: defaults
set From=peterpumpkineater@gmail.com
set To=arial@lavabit.com
set Subj="email test   %date% %time%"
set Body="did it work? %date% %time%"
set Serv=smtp.gmail.com
set Auth=peterpumpkineater
set Pass=password
set fileattach=

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Emailing with batch

#21 Post by Liviu » 23 Jul 2012 17:21

foxidrive wrote:It works for me using the code as I posted and this format

Interesting. I just rechecked using my gmail accounts, and:

- can't send over port 25 regardless of the SSL setting (FWIW there is a gmail advice to try 25 SSL as a last resort at http://support.google.com/mail/bin/answ ... swer=78775 under "still can't send"); I know for a fact that I don't have port 25 blocked by my ISP (some do) since other email accounts using it work fine;

- can send using 465 SSL, with either the full @gmail address or just the username part of it for authentication.

I can only guess that gmail, like others, might have different rules for different addresses grandfathered-in.

That said, I'd still recommend the official settings from http://support.google.com/mail/bin/answ ... swer=13287 - full address and 465 SSL.

Liviu

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Emailing with batch

#22 Post by foxidrive » 23 Jul 2012 17:49

This works with SSL and port 465

Code: Select all

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


:: defaults
set From=put_your_gmail_name_here@gmail.com
set To=receiver@lavabit.com
set Subj="email test   %date% %time%"
set Body="did it work? %date% %time%"
set Serv=smtp.gmail.com
set Auth=put_your_gmail_name_here
set Pass=put_your_gmail_password_here
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 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")   = 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") = 30
echo >>"%vbsfile%"  .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send
rem

Notelek_Labs
Posts: 16
Joined: 22 Jul 2012 20:26

Re: Emailing with batch

#23 Post by Notelek_Labs » 23 Jul 2012 19:28

WOOOOOOOOOOOOOO finally! I got it working
! Thanks for the help
Working Code:

Code: Select all

@echo off
setlocal

:: defaults
set From=noteleklabssecuredevices@gmail.com
set To=asdfjkl;@txt.att.net
set Subj="email test   %date% %time%"
set Body="did it work? %date% %time%"
set Serv=smtp.gmail.com
set Auth=noteleklabssecuredevices@gmail.com
set Pass=asdfjkl;
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%
pause
del "%vbsfile%" 2>nul
goto :EOF

:send
cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7 >nul 2>nul
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")   = 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
rem

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Emailing with batch

#24 Post by Squashman » 23 Jul 2012 20:02

Notelek_Labs wrote:WOOOOOOOOOOOOOO finally! I got it working
! Thanks for the help
Working Code:

Do you actually understand the SMTP settings now that create the VBscript?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Emailing with batch

#25 Post by Squashman » 26 Jul 2012 15:43

Has anyone gotten this to work with File attachments?

I have the file sitting in the same directory as the batch file.
I tried using the file name and the file name with the whole path
Both times it didn't even send the email.

Code: Select all

set fileattach=Test.txt
or
set fileattach="C:\Users\Squash\batch files\email\Test.txt"

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Emailing with batch

#26 Post by Squashman » 26 Jul 2012 15:49

Ignore the ID 10 T error.

I was double quoting. VBscript was putting quotes around the variable as well.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Emailing with batch

#27 Post by Ocalabob » 28 Jul 2012 15:01

Greetings All,

For what it's worth:

Editing the script by foxidrive using the lines presented in the code box I was
successful in sending email using Yahoo.com.

Code: Select all

set Serv=smtp.mail.yahoo.com

set Auth=your_full_address@yahoo.com

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

echo >>%vbsfile%  .Item ("%cdoSchema%/smtpusessl")       = False


Best wishes all!

Post Reply