Python and MS Exchange

Discussion in 'Python' started by Lindstrom Greg - glinds, Feb 12, 2004.

  1. I'm using python 2.3 and Windows 2000 "Professional" to access a Microsoft
    Exchange Server to monitor messages and perform various tasks around the
    office. My little program is attracted quite a bit of attention and I have
    been asked to add some features, but I don't know how to do a couple of
    things. Can anyone either help me out with the details, or point me to
    where I can get the information? Here's my task list:

    1. Create a folder for the mailbox. For example, I would like to have a
    "Processed" folder where I can move messages once they have been processed.

    2. How can I check to see if a folder exists?

    3. Can I access the allotted size of a folder to determine if I have room
    to post messages?

    4. How do I move a message to the folder I just created?

    Thanks for any help you can pass my way.

    Greg Lindstrom, IBCATS Development
    Acxiom Corporation
    (501) 342-1626

    We who cut mere stones must always be envisioning cathedrals. -- Quarry
    Worker's Creed

    The information contained in this communication is
    confidential, is intended only for the use of the recipient
    named above, and may be legally privileged.
    If the reader of this message is not the intended
    recipient, you are hereby notified that any dissemination,
    distribution, or copying of this communication is strictly
    If you have received this communication in error,
    please re-send this communication to the sender and
    delete the original message or any copy of it from your
    computer system. Thank You.
    Lindstrom Greg - glinds, Feb 12, 2004
    1. Advertisements

  2. Lindstrom Greg - glinds

    Mark Hammond Guest

    Can you tell us what object model you are using? Provide a little
    sample code to give us a clue.

    1) If you are yusing MAPI, there is a CreateFolder method.
    2) Try to open it, and if it fails, check the error code.
    3) This is probably in a property on the folder. Check the Exchange
    forums for what folder property you need.
    4) A "Move" method.

    Check out the SpamBayes project, and look at the source code to the
    Outlook Addin - in general, this uses MAPI (which is basically the
    'exchange object model'), but also does a few things via the Outlook
    object model - so you get a few examples of both. MAPI has a bigger
    learning curve, but is more powerful and faster.

    Mark Hammond, Feb 12, 2004
    1. Advertisements

  3. Lindstrom Greg - glinds

    Paul Boddie Guest

    And Outlook Explorer might also be useful as a starting point:

    There's just archiving going on in that program, but it could be more
    productive to look at its source code than it is to swim in the MSDN

    Paul Boddie, Feb 13, 2004
  4. Can you tell us what object model you are using? Provide a little
    Sure thing.

    from win32all.client import Dispatch

    mailbox = Dispatch("MAPI.session")
    mailbox.Login(profile='MS Exchange Settings')

    # Now *I* think I should be able to do the following

    # but I get...
    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    File "C:\Python23\lib\site-packages\win32com\client\",
    line 460, in __getattr__
    raise AttributeError, "%s.%s" % (self._username_, attr)
    AttributeError: MAPI.Session.listfolders

    # so, I've already taken a wrong turn!

    I've got the SpamBayes source code (BTW- I've been using SpamBayes for
    about 6 months and love it...very little SPAM gets through to me
    anymore) and will be looking it over this weekend.

    Is there a way to examine the mailbox object above to see what methods
    are available?

    Thanks for your help. I am keeping notes and plan to write up a
    summary for others.

    Greg Lindstrom, Feb 13, 2004
  5. Lindstrom Greg - glinds

    Mark Hammond Guest

    OK - you are using 'simple MAPI', and this is going to be a problem for
    you down the track. The biggest one will be the security dialog new
    versions Outlook use.

    This *is* an OK way to start, but don't go too far down this track - it
    is a dead end. On the positive side, it is possible to combine this
    'simple MAPI' with 'extended MAPI' (the latter is what SpamBayes uses)

    Simple MAPI is also known as "CDO"
    Note that what you have here is a 'session' object. Looking in the MSDN
    documentation for the CDO session object, there is no 'listfolders' method.

    What you probably want is something like:
    session = Dispatch("MAPI.session")
    inbox = session.Inbox
    print "Inbox name is", inbox.Name
    for i in range(inbox.Messages.Count):
    message = inbox.Messages.Item(i+1)
    # now 'message' has properties like 'Subject' etc

    See the CDO documentation for more details.

    Mark Hammond, Feb 13, 2004
  6. Mark (and others),

    Thanks for your time and attention. Since I expect to eventually want
    to do all sorts of things with this application, I think it's best to
    use MAPI (and not simple MAPI). I have the SpamBayes MAPIDriver (and
    everything else out of the "sandbox") and have stepped through it
    enough to figure most of it out. When I run "", I get
    the "MS Exchange Settings" (as I expected). When I run the
    "" it complains that "FalseGetAllItems" is not defined.
    Humph. When I attempt the " -p Subject test" it
    complains about not being able to find a default message store.

    I have read through a lot of the MSDN Docum
    Greg Lindstrom, Feb 16, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.