Jump to content

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

CDO COM - Email Delivery [AHK_L]


  • Please log in to reply
66 replies to this topic
shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
could you post your entire relevant code, so that i can try it on my machine.
also kindly mention your OS, 32/64bit and A_AHKVersion

The Naked General
  • Members
  • 21 posts
  • Last active: Jan 27 2012 02:41 AM
  • Joined: 22 Feb 2009
Sure,

Architecture: AMD Phenom 9850 Quad @ 2.51GHz
OS: Windows XP 64bit professional SP3 (also tested on xp32 sp3 intel chip)

A_AHKVERSION: 10.48.05.L61 (AHK_L)

FromAddress := "noreply@gmail.com"
ToAddress := "ToAddress@gmail.com"
MessageSubject := "MAPI TEST"
MessageBody := "This is a test of the MAPI hooked batch sender"
AttachmentPath := "c:\TEST.txt|c:\TEST.ico" ; can add multiple attachments, the delimiter is |

ol := ComObjCreate("Outlook.Application")
ns := ol.getNamespace("MAPI")
ns.logon("","",true,false)
newMail := ol.CreateItem(0)
newMail.Subject := MessageSubject
newMail.Body := MessageBody
MessageAttach := newMail.Attachments
Loop, Parse, AttachmentPath, |, %A_Space%%A_Tab%
MessageAttach.Add(A_LoopField)

; validate the recipient, just in case...
myRecipient := ns.CreateRecipient(ToAddress)
myRecipient.Resolve
If Not myRecipient.Resolved
MsgBox "unknown recipient"
Else
{
newMail.Recipients.Add(myRecipient)
newMail.Sender := FromAddress
newMail.Send
MsgBox mail sent!
}
Return

"lol, i made this thing, but it didn't work... so I read the forums and now it does!"

shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
The code you sent me is working perfectly on my machine. What version of Outlook are you using?

The Naked General
  • Members
  • 21 posts
  • Last active: Jan 27 2012 02:41 AM
  • Joined: 22 Feb 2009
OH! derp...
I have Outlook 2007 (12.0.4518.1014) and I was using hooks for 2010... thanks I never would have thought of that :D

Yeah that's what happens when I sleep between writing a script
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"

sinkfaze
  • Moderators
  • 6367 posts
  • Last active:
  • Joined: 18 Mar 2008
It seems now that JSON support is in it might be better to transition the attachment process to an array that can be traversed in a for-loop:

sAttach		:=	["Path_Of_Attachment"]	; can add multiple attachments to array e.g. ["Path_Of_Attachment1","Path_Of_Attachment2","Path_Of_Attachment3"]

[color=darkred]. . . [/color]

For each, item in sAttach
	pmsg.AddAttachment(item)


Da Prein
  • Guests
  • Last active:
  • Joined: --
Hi @ all I have tested every code and tried to fix it for me for example in the first code I had 2 Errors and marked them in the code can someone help?
pmsg          := ComObjCreate("CDO.Message")
pmsg.From       := """AHKUser"" <...@gmail.com>"
pmsg.To       := "anybody@somewhere.com"
pmsg.BCC       := ""   ; Blind Carbon Copy, Invisable for all, same syntax as CC
pmsg.CC       := "Somebody@somewhere.com, Other-somebody@somewhere.com"
pmsg.Subject    := "Message_Subject"

;You can use either Text or HTML body like
pmsg.TextBody    := "Message_Body"
;OR
;pmsg.HtmlBody := "<html><head><title>Hello</title></head><body><h2>Hello</h2><p>Testing!</p></body></html>"


sAttach         := "Path_Of_Attachment" ; can add multiple attachments, the delimiter is |

fields := Object()
fields.smtpserver   := "smtp.gmail.com" ; specify your SMTP server
fields.smtpserverport     := 465 ; 25
fields.smtpusessl      := True ; False
fields.sendusing     := 2   ; cdoSendUsingPort
fields.smtpauthenticate     := 1   ; cdoBasic
fields.sendusername := "...@gmail.com"
fields.sendpassword := "your_password_here"
fields.smtpconnectiontimeout := 60
schema := "http://schemas.microsoft.com/cdo/configuration/"


pfld :=   pmsg.Configuration.Fields

For field,value in fields
   pfld.Item(schema . field) := value
pfld.Update()

Loop, Parse, sAttach, |, %A_Space%%A_Tab%
  pmsg.AddAttachment(A_LoopField) <---- here
pmsg.Send() <---- and here
first Error was " the specified protocol is unkown"
the second after i fixed the first was "The message couldn´t be send to the SMTP-Server. The Serveranswer was "not available"
could it be that my SMTP-Server is wrong, because i don´t have gmail and tried to use it with gmx?
I only show that code because it was the one with only 2 Errors, i hope someone will answer and that the Topic isn´t too old.

Da Prein
  • Guests
  • Last active:
  • Joined: --
I have finally found the mistake and now get an e-mail, but in the e-mail are not the files i have attached.
I always only get a noname File with 0 Byte i can download.
thx for any help^^

azure
  • Members
  • 1216 posts
  • Last active: Mar 18 2015 09:06 AM
  • Joined: 07 Jun 2007
I want to assign some global hotkeys to these actions:

- send specific email body with specific subject to specific receipient
- the same as above, but opening a GUI to specify email body text
- the same as the first, but opening a GUI to specify attachment
etc

any hint?

zeus19
  • Members
  • 49 posts
  • Last active: Feb 24 2017 12:59 PM
  • Joined: 14 Apr 2011
Hi all,
Is there any way to check if the email has been sent or not?

zeus19
  • Members
  • 49 posts
  • Last active: Feb 24 2017 12:59 PM
  • Joined: 14 Apr 2011

Hi all,
Is there any way to check if the email has been sent or not?


Can somebody help me out with the question above?

wtg
  • Members
  • 251 posts
  • Last active: Dec 19 2012 03:54 PM
  • Joined: 04 Oct 2006
I don't think so. I believe the assumption is that if your mail server accepts the message - no errors while sending it to the server - then it's on it's way. Of course mail servers may queue messages up, delaying delivery, but I don't think there's a standard way to query a mail server to see if it's actually sent it's message yet.

Even if you could query the server, it wouldn't tell you whether the message has actually been received yet anyway since mail gets forwarded through many mail servers on the way to it's destination. The best you can do is to put a return receipt on the message, which of course itself isn't very reliable.

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: May 22 2017 11:15 PM
  • Joined: 15 Aug 2009
I'm getting the following error message:

---------------------------
email com.ahk
---------------------------
Error: 0x80040213 -
Source: CDO.Message.1
Description: The transport failed to connect to the server.


HelpFile: (null)
HelpContext: 0

Specifically: Send

Line#
029: schema := "http://schemas.microsoft.com/cdo/configuration/"
032: pfld := pmsg.Configuration.Fields
034: For field,value in fields
035: pfld.Item(schema . field) := value
036: pfld.Update()
038: Loop,Parse,sAttach,|,%A_Space%%A_Tab%
039: pmsg.AddAttachment(A_LoopField)
---> 040: pmsg.Send()
041: Exit
042: Exit
042: Exit

Continue running the script?
---------------------------
Yes No
---------------------------


I've tested the smtp server using Thunderbird and it worked. Can anyone tell me why I might be seeing this message and suggest solutions. Thanks.

wtg
  • Members
  • 251 posts
  • Last active: Dec 19 2012 03:54 PM
  • Joined: 04 Oct 2006
Is it a firewall issue perhaps?

Puzzled Greatly
  • Members
  • 612 posts
  • Last active: May 22 2017 11:15 PM
  • Joined: 15 Aug 2009

Is it a firewall issue perhaps?


Thanks, I don't think so. I'm using Windows Firewall and it isn't blocking outbound connections.

jpbirdsong
  • Members
  • 2 posts
  • Last active: Jun 14 2012 11:16 PM
  • Joined: 14 Jun 2012
sup cool people. first post from a lurker. below find a function based on of the very useful code developed here and elsewhere by sean, infogulch, sinkfaze, and the naked general.

things to notice:

[*:v2oeqwah]i've chosen to pass parameters using an object whose member objects can be parsed with 'for' loops (h/t sinkfaze) to simplify allowing an arbitrary number recipients and attachments.
[*:v2oeqwah]cc'ing and bcc'ing have been added
[*:v2oeqwah]supports html email
[*:v2oeqwah]option to view the email in outlook before it's sent
[*:v2oeqwah]i couldn't resolve the naked general's difficulties in setting a 'from' address, so that's not implemented. if i figure it out i'll update this post.
the function:
send_email(oEmail)
{
	ol := ComObjCreate("Outlook.Application")
	ns := ol.getNamespace("MAPI")
	ns.logon("Outlook","",true,true) ;get default MAPI profile
	newMail := ol.CreateItem(0) ;0 is Outlook constant 'olMailItem' (thanks sinkfaze)
	
	if oEmail.subject ;fill subject
		newMail.Subject := oEmail.subject 
	
	If oEmail.body ;fill body if not HTML
	{
		newMail.BodyFormat := 1 ; 1=olFormatPlain
		newMail.Body := oEmail.body
	}
	
	if oEmail.HTMLbody ;fill body if HTML
	{
		newMail.BodyFormat := 2 ; 2=olFormatHTML
		newMail.HTMLBody := oEmail.HTMLbody 
	}
	
	For each, item in oEmail.attachments ;add attachments
	{
		If item
			newMail.attachments.add(item)
	}
	
	bad_recipients := "" ; initialize var for function to return bad recipients 
	For each, item in oEmail.to ;add to recipients
	{
		If item
		{
			myRecipient := ns.CreateRecipient(item) ; validate the recipient before adding, just in case...
			myRecipient.Resolve
			If not myRecipient.Resolved
				bad_recipients .= item . ", "
			Else
			{
				newMail.Recipients.Add(item).Type := 1 ; 1="olTo"
			}
		}
	}
	
	For each, item in oEmail.cc ;add cc recipients
	{
		If item
		{
			myRecipient := ns.CreateRecipient(item)
			myRecipient.Resolve
			If not myRecipient.Resolved
				bad_recipients .= item . ", "
			Else
			{
				newMail.Recipients.Add(item).Type := 2 ; 2="olCC"
			}
		}
	}
	
	For each, item in oEmail.bcc ;add bcc recipients
	{
		If item
		{
			myRecipient := ns.CreateRecipient(item) 
			myRecipient.Resolve
			If not myRecipient.Resolved
				bad_recipients .= item . ", "
			Else
			{
				newMail.Recipients.Add(item).Type := 3 ; 3="olBCC"
			}
		}
	}
	
	If bad_recipients ;send (or, don't)
		Return "error - unknown recipient(s): " . bad_recipients
	else 
	{
		If oEmail.display ; display?
			newMail.display
		else
			newMail.Send
		Return "ok"
	}
}
example usage:
mail := {to:{}, cc:{}, bcc:{}, attachments:{}} ; need this to initialize object
mail.to.to_1 := "address1@domain1.com" ; remember to put everything in quotes
mail.to.to_2 := "address2@domain2.com" ; to add more recipients copy and paste the line and increment. Or, do it programatically...
mail.cc.cc_1 := "" ; it's ok to submit blanks, as the function tests for them; feel free to delete uneeded lines
mail.bcc.bcc_1 := ""
mail.subject := "subject"
mail.body := 
(
"plaintext body" 
)
mail.HTMLbody := 
(
"An HTML body supersedes a plaintext body.<br><b>testing bold</b><br><i>testing italics</i>"
)
mail.attachments.attachment_1 := "path1"
mail.attachments.attachment_2 := "path2"
mail.display := true ; note: function doesn't wait for you to click "send"!
error := send_email(mail)
If error!=ok
	msgbox %error%
so, yeah; i hope that works & is useful to somebody. thanks for the amazing work that y'all do.