Discussion:
[python-win32] getting email adresses from outlook
Jürgen Kareta
2005-05-31 12:01:53 UTC
Permalink
Hello,

I'm trying to get some email addresses with the following pythoncode:

import win32com.client
O = win32com.client.gencache.EnsureDispatch('Outlook.Application')
mapi=O.GetNamespace('MAPI')
adr_li=mapi.AddressLists.Item('Global Addressbook')
members=adr_li.AddressEntries.Item('MyGroup').Members
for num in range(0, members.__len__()-1):
name=entr.GetNext()
print name.Name,name.Address

That works fine, except that I get the x400 address:
paul panther /o=panter group/ou=venus/cn=Recipients/cn=PaulP
instead of the expected SMTP adress:
***@pantergoup.com

How can I get the SMPT addresses ?

regards,
Jürgen
Steve Holden
2005-05-31 12:46:26 UTC
Permalink
Post by Jürgen Kareta
Hello,
import win32com.client
O = win32com.client.gencache.EnsureDispatch('Outlook.Application')
mapi=O.GetNamespace('MAPI')
adr_li=mapi.AddressLists.Item('Global Addressbook')
members=adr_li.AddressEntries.Item('MyGroup').Members
name=entr.GetNext()
print name.Name,name.Address
entr.GetNext? Is it possible you aren't dealing with what you think you
are dealing with? Don't see any other reference to this in your code.
Post by Jürgen Kareta
paul panther /o=panter group/ou=venus/cn=Recipients/cn=PaulP
How can I get the SMPT addresses ?
members = "iterable"
members.__len__()
8
Post by Jürgen Kareta
len(members)
8
Post by Jürgen Kareta
range(0, 8-1)
[0, 1, 2, 3, 4, 5, 6]
Post by Jürgen Kareta
range(8)
[0, 1, 2, 3, 4, 5, 6, 7]
Post by Jürgen Kareta
[members[x] for x in range(0, members.__len__()-1)]
['i', 't', 'e', 'r', 'a', 'b', 'l']
^^^
??? no "e" ???
Post by Jürgen Kareta
[x for x in members]
['i', 't', 'e', 'r', 'a', 'b', 'l', 'e']
Try:

for num in range(len(members)):
name=entr.GetNext()
print name.Name,name.Address

Seems to me like the X.400 address may be just the first one, and your
code is accidentally not handling the last (second, Internet) address.

If members is being wrapped by pythoncom as a Python iterable then you
might be able to get away with (something like, untested):

for entry in adr_li.AddressEntries.Item('MyGroup').Members:
for name in entry.Members:
print name.Name,name.Address

HTH. Just a few ideas.

regards
Steve
--
Steve Holden +1 703 861 4237 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Jürgen Kareta
2005-05-31 13:20:49 UTC
Permalink
Post by Steve Holden
Post by Jürgen Kareta
Hello,
import win32com.client
O = win32com.client.gencache.EnsureDispatch('Outlook.Application')
mapi=O.GetNamespace('MAPI')
adr_li=mapi.AddressLists.Item('Global Addressbook')
members=adr_li.AddressEntries.Item('MyGroup').Members
name=entr.GetNext()
print name.Name,name.Address
entr.GetNext? Is it possible you aren't dealing with what you think
you are dealing with? Don't see any other reference to this in your code.
sorry, entr.GetNext should be members.GetNext

name=
'<win32com.gen_py.Microsoft Outlook 9.0 Object Library.AddressEntry
instance at 0x20713680> '

dir(name) =
['CLSID', 'Delete', 'Details', 'GetFreeBusy', 'Update',
'UpdateFreeBusy', '_ApplyTypes_', '__cmp__', '__doc__', '__getattr__',
'__init__', '__module__', '__repr__', '__setattr__',
'_get_good_object_', '_get_good_single_object_', '_oleobj_',
'_prop_map_get_', '_prop_map_put_', 'coclass_clsid']

name._prop_map_get_ =
{'Name': (12289, 2, (8, 0), (), 'Name', None),
'Parent': (61441, 2, (9, 0), (), 'Parent', None),
'DisplayType': (14592, 2, (3, 0), (), 'DisplayType', None),
'MAPIOBJECT': (61696, 2, (13, 0), (), 'MAPIOBJECT', None),
'Class': (61450, 2, (3, 0), (), 'Class', None),
'Application': (61440, 2, (9, 0), (), 'Application',
'{00063001-0000-0000-C000-000000000046}'),
'Manager': (771, 2, (9, 0), (), 'Manager',
'{0006304B-0000-0000-C000-000000000046}'),
'Session': (61451, 2, (9, 0), (), 'Session',
'{00063002-0000-0000-C000-000000000046}'),
'Members': (772, 2, (9, 0), (), 'Members',
'{0006304A-0000-0000-C000-000000000046}'),
'Address': (12291, 2, (8, 0), (), 'Address', None),
'Type': (12290, 2, (8, 0), (), 'Type', None),
'ID': (61470, 2, (8, 0), (), 'ID', None)}

I found an example on the net for reading/writing contacts with python.
There is a property 'AddressType' with value 'SMTP'
available. But here, I can't found anything similar.

regards,
Jürgen
Simon Brunning
2005-05-31 13:20:30 UTC
Permalink
Post by Jürgen Kareta
Hello,
import win32com.client
O = win32com.client.gencache.EnsureDispatch('Outlook.Application')
mapi=O.GetNamespace('MAPI')
adr_li=mapi.AddressLists.Item('Global Addressbook')
members=adr_li.AddressEntries.Item('MyGroup').Members
name=entr.GetNext()
print name.Name,name.Address
paul panther /o=panter group/ou=venus/cn=Recipients/cn=PaulP
How can I get the SMPT addresses ?
Anything here help - <http://www.cdolive.com/cdo10.htm>?
--
Cheers,
Simon B,
***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
Jürgen Kareta
2005-06-01 10:47:00 UTC
Permalink
Hi Simon,

thanks for the useful link. I think that should help me out, specialy as
I found a mapi viewer on the net wich shows the nessesary ids.
I need the CDO com object. But I get errors, when I trie to open the com
object.

from win32com.client.dynamic import Dispatch
s=Dispatch("Mapi.session")

results in
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line
79, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221005, 'Ung\xfcltige Klassenzeichenfolge', None, None)

So it seems to be that mapi/cdo is not or not correct installed on my
machine (xp pro sp2). Can anybody tell me, wich libary is needed to
support that com object ?

thanks in advance,
Jürgen
Mark Hammond
2005-06-01 12:19:31 UTC
Permalink
Post by Jürgen Kareta
results in
File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line
79, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx,
pythoncom.IID_IDispatch)
com_error: (-2147221005, 'Ung\xfcltige Klassenzeichenfolge',
None, None)
So it seems to be that mapi/cdo is not or not correct installed on my
machine (xp pro sp2).
I'm guessing that MAPI is installed, but SP2 is preventing "script"
(automation) access.

I know that well before XP SP2, the SpamBayes project moved away from the
high-level Mapi.Session objects towards the low-level win32com.mapi
interfaces for similar reasons (eg, an Outlook 2000 SP would cause a
confirmation dialog before allowing access to these Mapi.Session objects -
the low-level MAPI functions avoided this.) I'm fairly confident that these
low-level interfaces work in XP SP2, as SpamBayes apparently does.

Unfortunately there is a steep learning curve related to using these
interfaces, but on the upside their performance is significantly better ;)

Mark
Jürgen Kareta
2005-06-01 12:44:52 UTC
Permalink
Hello,

I've solved my problem. After adding CDO to my Outlook installation it
works now:

from win32com.client.dynamic import Dispatch
s=Dispatch("Mapi.session")
s.Logon('Microsoft Outlook')
entries=s.AddressLists('Globales Adressbuch').AddressEntries
for entr in entries:
print entr.Name,entr.Fields(0x39fe001e).Value

Mark and Simon:
thanks for your useful help. I'll look for the spambayes solution, as
this code is only the first step for a bigger solution.

regards,
Jürgen

Continue reading on narkive:
Loading...