Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python To Send Emails Via Outlook Express

Reply
Thread Tools

Python To Send Emails Via Outlook Express

 
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-21-2004
Heavy sigh... <grin>

======
This script WILL send the email
import win32com.client


s = win32com.client.Dispatch('CDO.Message')
c = win32com.client.Dispatch('CDO.Configuration')
cdoSourceOutlookExpress = 2
c.Load(cdoSourceOutlookExpress)
s.Configuration = c
s.From = "(E-Mail Removed)"
s.To = "(E-Mail Removed)"
s.Subject = "The subject"


s.Send()
======


======
But if a change the TO email address to a yahoo address the server
rejects it
import win32com.client


s = win32com.client.Dispatch('CDO.Message')
c = win32com.client.Dispatch('CDO.Configuration')
cdoSourceOutlookExpress = 2
c.Load(cdoSourceOutlookExpress)
s.Configuration = c
s.From = "(E-Mail Removed)"
s.To = "(E-Mail Removed)"
s.Subject = "The subject"


s.Send()
======

It's official. I have given up sending emails any other way but via
smtp.

 
Reply With Quote
 
 
 
 
Lenard Lindstrom
Guest
Posts: n/a
 
      12-21-2004
Steve Holden <(E-Mail Removed)> writes:

> http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> > Hi Fredrik,
> > Thank you for the suggestion.
> > I tried different from/to settings and guess what? The mail came
> > thru.
> > The script is now..
> > import win32com.client
> > s = win32com.client.Dispatch('CDO.Message')
> > s.From = "(E-Mail Removed)" (was
> > "(E-Mail Removed)")
> > s.To = "(E-Mail Removed)" (was
> > "(E-Mail Removed)")
> > s.Subject = "The subject"
> > s.Send()
> > My problem is thought, the message is still not being sent via
> > Outlook
> > Express.
> > What am I missing?
> > Thanks again for your help so far!!
> > Kind regards
> > Ian
> >

> Unfortunately Outlook Express isn't programmable in the same way as
> Outlook. I used to use it because this property gave it a certain
> degree of immunity from macro viruses (back in the days when Outlook
> came configured to open any Office document it came across).
>

Outlook Express can be accessed through the Simple Mapi interface -
at least the version on Win98 can. I have a program that reads
and deletes messages from the new mail folder. So I would imaging
mail can be also sent through Outlook Express using Simple Mapi,
but I have not tried it. I have also not tried using Simple Mapi
under Python, only VC++. But a quick test shows I can access
MAPI32.DLL use the ctypes package:

import ctypes
mapi = ctypes.windll.mapi32
MAPILogon = mapi.MAPILogon
....

It is messy though.

Lenard Lindstrom
<(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-21-2004
That sound really promising. Is there any chance you could forward me a
copy of the script. I'm still very new to Python and it would help me a
lot.
Thanks again

Ian

 
Reply With Quote
 
Lenard Lindstrom
Guest
Posts: n/a
 
      12-22-2004
(E-Mail Removed) writes:

> That sound really promising. Is there any chance you could forward me a
> copy of the script. I'm still very new to Python and it would help me a
> lot.
> Thanks again
>
> Ian


This is a simple example I have put together:

=============== SimpleMAPI.py ======================
# module SimpleMAPI

from ctypes import *

FLAGS = c_ulong
LHANDLE = c_ulong
LPLHANDLE = POINTER(LHANDLE)

# Return codes
SUCCESS_SUCCESS = 0
# Recipient class
MAPI_ORIG = 0
MAPI_TO = 1

NULL = c_void_p(None)

class MapiRecipDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('ulRecipClass', c_ulong),
('lpszName', c_char_p),
('lpszAddress', c_char_p),
('ulEIDSize', c_ulong),
('lpEntryID', c_void_p),
]
lpMapiRecipDesc = POINTER(MapiRecipDesc)

class MapiFileDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('flFlags', c_ulong),
('nPosition', c_ulong),
('lpszPathName', c_char_p),
('lpszFileName', c_char_p),
('lpFileType', c_void_p),
]
lpMapiFileDesc = POINTER(MapiFileDesc)

class MapiMessage(Structure):
_fields_ = [('ulReserved', c_ulong),
('lpszSubject', c_char_p),
('lpszNoteText', c_char_p),
('lpszMessageType', c_char_p),
('lpszDateReceived', c_char_p),
('lpszConversationID', c_char_p),
('flFlags', FLAGS),
('lpOriginator', lpMapiRecipDesc), # ignored?
('nRecipCount', c_ulong),
('lpRecips', lpMapiRecipDesc),
('nFileCount', c_ulong),
('lpFiles', lpMapiFileDesc),
]
lpMapiMessage = POINTER(MapiMessage)

MAPI = windll.mapi32

MAPISendMail=MAPI.MAPISendMail
MAPISendMail.restype = c_ulong # Error code
MAPISendMail.argtypes = (LHANDLE, # lhSession
c_ulong, # ulUIParam
lpMapiMessage, # lpMessage
FLAGS, # lpFlags
c_ulong, # ulReserved
)

def SendMail(recipient, subject, body):
"""Post an e-mail message using Simple MAPI

recipient - string: address to send to
subject - string: subject header
body - string: message text
"""

recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
0, cast(NULL, lpMapiFileDesc))
rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc
================= Example usage =========================
import SimpleMAPI
SimpleMAPI.SendMail("(E-Mail Removed)",
"The subject line"
"This is the message content.\n")


Lenard Lindstrom
<(E-Mail Removed)>

 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-22-2004
Hi Lenard,
Absolutely fantastic!!
That worked like a charm.
Now onto adapting it to send attachments.

Thanks again

Ian

 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-22-2004
Hi Lenard,
Absolutely fantastic!!
That worked like a charm.
Now onto adapting it to send attachments.

Thanks again

Ian

 
Reply With Quote
 
Lenard Lindstrom
Guest
Posts: n/a
 
      12-22-2004
(E-Mail Removed) writes:

> Hi Lenard,
> Absolutely fantastic!!
> That worked like a charm.
> Now onto adapting it to send attachments.
>

Glad to be of help.

Lenard Lindstrom
 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-23-2004
Hi Lenard,

As the risk of severely embarassing myself can I ask for your help one
more time.
I have tried changing your script to include attachments, but guess
what, (and this should come as no suprise) I can't do it.
So....
Here is my feeble attempt at changing the script..
----------------------------------------------------------------------------------------
import os
from ctypes import *

FLAGS = c_ulong
LHANDLE = c_ulong
LPLHANDLE = POINTER(LHANDLE)


# Return codes
SUCCESS_SUCCESS = 0
# Recipient class
MAPI_ORIG = 0
MAPI_TO = 1


NULL = c_void_p(None)


class MapiRecipDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('ulRecipClass', c_ulong),
('lpszName', c_char_p),
('lpszAddress', c_char_p),
('ulEIDSize', c_ulong),
('lpEntryID', c_void_p),
]
lpMapiRecipDesc = POINTER(MapiRecipDesc)


class MapiFileDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('flFlags', c_ulong),
('nPosition', c_ulong),
('lpszPathName', c_char_p),
('lpszFileName', c_char_p),
('lpFileType', c_void_p),
]
lpMapiFileDesc = POINTER(MapiFileDesc)


class MapiMessage(Structure):
_fields_ = [('ulReserved', c_ulong),
('lpszSubject', c_char_p),
('lpszNoteText', c_char_p),
('lpszMessageType', c_char_p),
('lpszDateReceived', c_char_p),
('lpszConversationID', c_char_p),
('flFlags', FLAGS),
('lpOriginator', lpMapiRecipDesc), # ignored?
('nRecipCount', c_ulong),
('lpRecips', lpMapiRecipDesc),
('nFileCount', c_ulong),
('lpFiles', lpMapiFileDesc),
]
lpMapiMessage = POINTER(MapiMessage)


MAPI = windll.mapi32


MAPISendMail=MAPI.MAPISendMail
MAPISendMail.restype = c_ulong # Error code
MAPISendMail.argtypes = (LHANDLE, # lhSession
c_ulong, # ulUIParam
lpMapiMessage, # lpMessage
FLAGS, # lpFlags
c_ulong, # ulReserved
)


def SendMail(recipient, subject, body, attach=[]):
"""Post an e-mail message using Simple MAPI


recipient - string: address to send to
subject - string: subject header
body - string: message text
attach - string: files to attach
"""
attach = map( os.path.abspath, attach )
nFileCount = len(attach)
if attach:
MapiFileDesc_A = MapiFileDesc * len(attach)
fda = MapiFileDesc_A()
for fd, fa in zip(fda, attach):
fd.ulReserved = 0
fd.flFlags = 0
fd.nPosition = -1
fd.lpszPathName = fa
fd.lpszFileName = None
fd.lpFileType = None
lpFiles = fda


recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
#msg = MapiMessage(0, subject, body, None, None, None, 0,
# cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
# nFileCount, cast(NULL, lpMapiFileDesc))
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
nFileCount, cast(NULL, lpFiles))


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc

 
Reply With Quote
 
Lenard Lindstrom
Guest
Posts: n/a
 
      12-23-2004
(E-Mail Removed) writes:

> Hi Lenard,
>
> As the risk of severely embarassing myself can I ask for your help one
> more time.
> I have tried changing your script to include attachments, but guess
> what, (and this should come as no suprise) I can't do it.
> So....
> Here is my feeble attempt at changing the script..


Actually the only real problem is with the lpFiles argument
to the MapiMessage constructor call.

> ----------------------------------------------------------------------------------------

[snip]
>
> def SendMail(recipient, subject, body, attach=[]):
> """Post an e-mail message using Simple MAPI
>
>
> recipient - string: address to send to
> subject - string: subject header
> body - string: message text
> attach - string: files to attach
> """
> attach = map( os.path.abspath, attach )
> nFileCount = len(attach)
> if attach:
> MapiFileDesc_A = MapiFileDesc * len(attach)
> fda = MapiFileDesc_A()
> for fd, fa in zip(fda, attach):
> fd.ulReserved = 0
> fd.flFlags = 0
> fd.nPosition = -1
> fd.lpszPathName = fa
> fd.lpszFileName = None
> fd.lpFileType = None
> lpFiles = fda


Add this else clause to the "if attach:"

else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

>
>
> recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
> #msg = MapiMessage(0, subject, body, None, None, None, 0,
> # cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
> # nFileCount, cast(NULL, lpMapiFileDesc))
> msg = MapiMessage(0, subject, body, None, None, None, 0,
> cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
> nFileCount, cast(NULL, lpFiles))


Replace "cast(NULL, lpFiles)" with "lpFiles".
>
>
> rc = MAPISendMail(0, 0, byref(msg), 0, 0)
> if rc != SUCCESS_SUCCESS:
> raise WindowsError, "MAPI error %i" % rc


And that is it.

The "cast(NULL, lpFiles)" was a way of assigning a null value to
the lpFiles member of the MapiMessage structure. Using None did
not work. But recasting a c_void_p(None) did.

Lenard Lindstrom
<(E-Mail Removed)>
 
Reply With Quote
 
ian@kirbyfooty.com
Guest
Posts: n/a
 
      12-24-2004
Hello again,
Thanks for the advice!
Unfortunately I still cannot get it to send attachments.
It comes up with the following windows error..
(I have a feeling it has something to do with the file count)

>>> import simplemapi
>>> simplemapi.SendMail("(E-Mail Removed)","The Subject","The

body","c:\ian\ian.txt")
nFileCount 14
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "simplemapi.py", line 111, in SendMail
raise WindowsError, "MAPI error %i" % rc
WindowsError: MAPI error 2


This is the updated script..

----------------------------------
import os
from ctypes import *

FLAGS = c_ulong
LHANDLE = c_ulong
LPLHANDLE = POINTER(LHANDLE)


# Return codes
SUCCESS_SUCCESS = 0
# Recipient class
MAPI_ORIG = 0
MAPI_TO = 1


NULL = c_void_p(None)


class MapiRecipDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('ulRecipClass', c_ulong),
('lpszName', c_char_p),
('lpszAddress', c_char_p),
('ulEIDSize', c_ulong),
('lpEntryID', c_void_p),
]
lpMapiRecipDesc = POINTER(MapiRecipDesc)


class MapiFileDesc(Structure):
_fields_ = [('ulReserved', c_ulong),
('flFlags', c_ulong),
('nPosition', c_ulong),
('lpszPathName', c_char_p),
('lpszFileName', c_char_p),
('lpFileType', c_void_p),
]
lpMapiFileDesc = POINTER(MapiFileDesc)


class MapiMessage(Structure):
_fields_ = [('ulReserved', c_ulong),
('lpszSubject', c_char_p),
('lpszNoteText', c_char_p),
('lpszMessageType', c_char_p),
('lpszDateReceived', c_char_p),
('lpszConversationID', c_char_p),
('flFlags', FLAGS),
('lpOriginator', lpMapiRecipDesc), # ignored?
('nRecipCount', c_ulong),
('lpRecips', lpMapiRecipDesc),
('nFileCount', c_ulong),
('lpFiles', lpMapiFileDesc),
]
lpMapiMessage = POINTER(MapiMessage)


MAPI = windll.mapi32


MAPISendMail=MAPI.MAPISendMail
MAPISendMail.restype = c_ulong # Error code
MAPISendMail.argtypes = (LHANDLE, # lhSession
c_ulong, # ulUIParam
lpMapiMessage, # lpMessage
FLAGS, # lpFlags
c_ulong, # ulReserved
)


def SendMail(recipient, subject, body, attach=[]):
"""Post an e-mail message using Simple MAPI


recipient - string: address to send to
subject - string: subject header
body - string: message text
attach - string: files to attach
"""
attach = map( os.path.abspath, attach )
nFileCount = len(attach)
if attach:
MapiFileDesc_A = MapiFileDesc * len(attach)
fda = MapiFileDesc_A()
for fd, fa in zip(fda, attach):
fd.ulReserved = 0
fd.flFlags = 0
fd.nPosition = -1
fd.lpszPathName = fa
fd.lpszFileName = None
fd.lpFileType = None
lpFiles = fda
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

print "nFileCount ",nFileCount


recip = MapiRecipDesc(0, MAPI_TO, None, recipient, 0, None)
#msg = MapiMessage(0, subject, body, None, None, None, 0,
# cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
# nFileCount, cast(NULL, lpMapiFileDesc))
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc), 1, pointer(recip),
nFileCount, lpFiles)


rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS:
raise WindowsError, "MAPI error %i" % rc

--------------------------

Thanks again for your help so far on this. I really appreciate it!
Have a safe and very merry Christmas.

God bless!!

Ian

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Send To > Mail Recipient opens Outlook Express instead of Outlook 2003 rvp@ncf.ca Windows 64bit 6 09-06-2012 07:26 AM
Receiving emails in Outlook and outlook Express reboots computer. J-McC Computer Support 1 07-11-2007 07:46 AM
I can't get Outlook Express and XP Pro to play togehter - Outlook Express just doesn't start. Marc Computer Support 5 08-22-2006 10:46 PM
unable to send emails on outlook express tracey ellis Computer Support 4 02-24-2006 01:49 PM
ctypes NULL pointers; was: Python To Send Emails Via Outlook Express Lenard Lindstrom Python 3 01-04-2005 05:29 AM



Advertisments