Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

CDO COM - Email Delivery


  • Please log in to reply
212 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
In this script, Email is sent using GMail SMTP (SSL) server.

NEED: COM Standard Library.

sFrom	  := "Mail_Address_Of_Sender"
sTo	    := "Mail_Address_Of_Receipient"
sSubject  := "Message_Subject"
sBody	  := "Message_Text"
sAttach	:= "Path_Of_Attachment" ; can add multiple attachments, the delimiter is [color=red]|[/color]

sServer   := "smtp.gmail.com" ; specify your SMTP server
nPort     := 465 ; 25
bTLS      := True ; False
nSend     := 2	; cdoSendUsingPort
nAuth     := 1	; cdoBasic
sUsername := "your_username"
sPassword := "your_password"

COM_Init()
pmsg :=	COM_CreateObject("CDO.Message")
pcfg :=	COM_Invoke(pmsg, "Configuration")
pfld :=	COM_Invoke(pcfg, "Fields")

COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusing", nSend)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 60)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserver", sServer)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserverport", nPort)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpusessl", bTLS)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", nAuth)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusername", sUsername)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendpassword", sPassword)
COM_Invoke(pfld, "Update")

COM_Invoke(pmsg, "From", sFrom)
COM_Invoke(pmsg, "To", sTo)
COM_Invoke(pmsg, "Subject", sSubject)
COM_Invoke(pmsg, "TextBody", sBody)
Loop, Parse, sAttach, |, %A_Space%%A_Tab%
COM_Invoke(pmsg, "AddAttachment", A_LoopField)
COM_Invoke(pmsg, "Send")

COM_Release(pfld)
COM_Release(pcfg)
COM_Release(pmsg)
COM_Term()


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
It does not work for me Sean :(
Edit: Sorry! the following script works flawlessly.
I am posting the exact script with valid email-id's / username and password:

StringReplace, Attachment, A_AhkPath, AutoHotkey.exe, License.txt

sFrom     := "[email protected]"
sTo       := "[email protected]"
sSubject  := "GNU Public License"
sText     := "Please Read The Attachment"
sAttach   := Attachment

sServer   := "smtp.gmail.com" ; specify your SMTP server
nPort     := 465 ; 25
bTLS      := True ; False
sUsername := "ahkuser"
sPassword := "ahkuser1"

COM_Init()
pmsg :=   COM_CreateObject("CDO.Message")
pcfg :=   COM_Invoke(pmsg, "Configuration")
pfld :=   COM_Invoke(pcfg, "Fields")

COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusing", 2)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 60)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserver", sServer)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserverport", nPort)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpusessl", bTLS)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusername", sUsername)
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendpassword", sPassword)
COM_Invoke(pfld, "Update")

COM_Invoke(pmsg, "From", sFrom)
COM_Invoke(pmsg, "To", sTo)
COM_Invoke(pmsg, "Subject", sSubject)
COM_Invoke(pmsg, "TextBody", sText)
COM_Invoke(pmsg, "AddAttachment", sAttach)
COM_Invoke(pmsg, "Send")

COM_Release(pfld)
COM_Release(pcfg)
COM_Release(pmsg)
COM_Term()

I updated my copy of COM in UserLib. The TrayIcon is seen for a few seconds. But no joy in my inbox.

What Am I missing?

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

It does not work for me Sean :(

Was there no error message? It usually produces an error at the stage of the function Send if fails. Anyway, I suspect it's the attachment. GMail doesn't allow an executable file as an attachment, changing the file name doesn't work either, at least with me. So, I myself always compress/zip it, then send. I suggest to try the same.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

Was there no error message? It usually produces an error at the stage of the function Send if fails.


No error at all. It takes the usual few seconds like with blat... only that blat cannot handle gmail and hence I want this script verryy much.

Anyway, I suspect it's the attachment. GMail doesn't allow an executable file as an attachment, changing the file name doesn't work either, at least with me. So, I myself always compress/zip it, then send. I suggest to try the same.


I am attaching only a text file. Iam using StringReplace to derive the path to the following text file:
C:\Program Files\AutoHotkey\License.txt

Please help. I would love to use GMail programmatically.. ( So would many )

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

No error at all.

Then, it's hard to tell where locates the loophole. What's the OS btw? The only suggestion I can think of atm is changing the port number from 465 to 587. In fact, 587 is my preferred port with GMail, however, didn't work in my XPSP3 so used 465 instead. And, although it's unlikely, may use full account name as the username.
[email protected]


SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Dear Sean, :)

Then, it's hard to tell where locates the loophole.


I was checking ahkuser instead on my account. :oops: ( I need rest, badly! )
The mails were sent properly. Very sorry for the inconvenience.

I tested in XP SP2.

I cannot thank you enough for this, Sean. I've been searching for such a solution over 2 years. Thank you SOoo much! :D

Regards, :)
kWo4Lk1.png

n-l-i-d
  • Guests
  • Last active:
  • Joined: --
Excellent indeed!

One question though: what are the links in the code for?

http://schemas.microsoft.com/cdo/configuration/sendusing


They all lead to 404 error pages.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

I was checking ahkuser instead on my account. :oops: ( I need rest, badly! )

That happens sometimes.

The mails were sent properly.

Glad to hear that.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

One question though: what are the links in the code for?

http://schemas.microsoft.com/cdo/configuration/sendusing

That's just the way they locate the fields, i.e., namespace.
<!-- m -->http://msdn.microsof...y/ms527274.aspx<!-- m -->

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
Wow Sean, very nice. I have seen many requests for AHK code to send email without external apps, and here we have one that can even handle gmail, unlike blat. Great work!

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Wow Sean, very nice. I have seen many requests for AHK code to send email without external apps, and here we have one that can even handle gmail, unlike blat. Great work!

Thanks. I forgot to update the script to allow multiple attachments, so done it now. The delimiter is |.

tank
  • Administrators
  • 4345 posts
  • AutoHotkey Foundation
  • Last active: May 02 2019 09:16 PM
  • Joined: 21 Dec 2007
exchange?
instead of smtp would i use my exchange server im a bit dense on mail protocols
Never lose.
WIN or LEARN.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

exchange?
instead of smtp would i use my exchange server im a bit dense on mail protocols

I don't think cdosys.dll defaultly installed on W2K higher supports using exchange. But if cdoex.dll is installed in your system, you can use exchange via cdoSendUsingExchange.
<!-- m -->http://msdn.microsof...y/aa579557.aspx<!-- m -->

Yetti
  • Members
  • 6 posts
  • Last active: Jan 21 2009 06:05 PM
  • Joined: 18 Dec 2008
#include COM.ahk

sFrom     := "[email protected]" 
sTo       := "[email protected]" 
sSubject  := "GNU Public License" 
sText     := "Please Read The Attachment" 
sAttach   := "C:\License.zip" 

sServer   := "smtp.list.ru" ; specify your SMTP server 
nPort     := 2525 ; 25 
bTLS      := True ; False 
sUsername := "[email protected]" 
sPassword := "*******" 

COM_Init() 
pmsg :=   COM_CreateObject("CDO.Message") 
pcfg :=   COM_Invoke(pmsg, "Configuration") 
pfld :=   COM_Invoke(pcfg, "Fields") 

COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusing", 2) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 60) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserver", sServer) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpserverport", nPort) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpusessl", bTLS) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendusername", sUsername) 
COM_Invoke(pfld, "Item", "http://schemas.microsoft.com/cdo/configuration/sendpassword", sPassword) 
COM_Invoke(pfld, "Update") 

COM_Invoke(pmsg, "From", sFrom) 
COM_Invoke(pmsg, "To", sTo) 
COM_Invoke(pmsg, "Subject", sSubject) 
COM_Invoke(pmsg, "TextBody", sText) 
COM_Invoke(pmsg, "AddAttachment", sAttach) 
COM_Invoke(pmsg, "Send")

COM_Release(pfld) 
COM_Release(pcfg) 
COM_Release(pmsg) 
COM_Term()
Posted Image
It does not work for me Sean :(
I tested in XP SP3.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

It does not work for me Sean :(
I tested in XP SP3.

I'm on XPSP3 too. Anyway, the cdo error code is
CDO_E_FAILED_TO_CONNECT := 0x80040213
So it seems to be your network connection problem. How big is your attachment? I suggest to test first without an attachement.