[python-win32] How to send mail via Extended MAPI

David Fraser davidf at sjsoft.com
Thu Aug 19 17:29:15 CEST 2004


Hi

I'm sure this code will be useful to others 'cause it took some blood 
sweat and tears to work out ...
Perhaps it could be included (in some form) in the win32 mapi classes?

David

PS You can send mail easily using the Simple MAPI COM wrappers but it 
has several problems with various patches for Outlook that bring up 
dialogs etc.

-------------- next part --------------
#!/usr/bin/env python

"""this sample code sends mail with Extended MAPI using the pywin32 mapi wrappers..."""

from win32com.mapi import mapi
from win32com.mapi import mapitags
import types

def SendEMAPIMail(Subject="", Message="", SendTo=None, SendCC=None, SendBCC=None, MAPIProfile=None):
    """
    Sends an email to the recipient using the extended MAPI interface
    """

    # initialize and log on
    mapi.MAPIInitialize(None)
    session = mapi.MAPILogonEx(0, MAPIProfile, None, mapi.MAPI_EXTENDED | mapi.MAPI_USE_DEFAULT)
    adminprofiles = mapi.MAPIAdminProfiles(0)
    messagestorestable = session.GetMsgStoresTable(0)
    statustable = session.GetStatusTable(0)
    messagestorestable.SetColumns((mapitags.PR_ENTRYID, mapitags.PR_DISPLAY_NAME_A, mapitags.PR_DEFAULT_STORE),0)

    while (True):
        rows = messagestorestable.QueryRows(1, 0)
        if len(rows) != 1:
            break
        row = rows[0]
        property,value = row[0]
        if (property == mapitags.PR_DEFAULT_STORE and value == True):
            break

    # unpack the row and open the message store
    (eid_tag, eid), (name_tag, name), (def_store_tag, def_store) = row
    msgstore = session.OpenMsgStore(0,eid,None,mapi.MDB_NO_DIALOG | mapi.MAPI_BEST_ACCESS)

    # get the outbox
    hr, props = msgstore.GetProps((mapitags.PR_IPM_OUTBOX_ENTRYID), 0)
    (tag, eid) = props[0]
    outboxfolder = msgstore.OpenEntry(eid,None,mapi.MAPI_BEST_ACCESS)

    # create the message and the addrlist
    message = outboxfolder.CreateMessage(None,0)
   
    pal = []
    def makeentry(recipient, recipienttype):
      return ((mapitags.PR_RECIPIENT_TYPE, recipienttype),
              (980484099, 0),
              (mapitags.PR_SEND_RICH_INFO, False),
              (mapitags.PR_DISPLAY_TYPE, 0),
              (mapitags.PR_OBJECT_TYPE, 6),
              (mapitags.PR_EMAIL_ADDRESS_A, recipient),
              (mapitags.PR_ADDRTYPE_A, 'SMTP'),
              (mapitags.PR_DISPLAY_NAME_A, recipient))
    
    if SendTo:
      pal.extend([makeentry(recipient, mapi.MAPI_TO) for recipient in SendTo.split(",")])
    if SendCC:
      pal.extend([makeentry(recipient, mapi.MAPI_CC) for recipient in SendCC.split(",")])
    if SendBCC:
      pal.extend([makeentry(recipient, mapi.MAPI_BCC) for recipient in SendBCC.split(",")])

    # add the resolved recipients to the message
    message.ModifyRecipients(mapi.MODRECIP_ADD,pal)
    message.SetProps([(mapitags.PR_BODY_A,Message),
                      (mapitags.PR_SUBJECT_A,Subject)])

    # save changes and submit
    outboxfolder.SaveChanges(0)
    message.SubmitMessage(0)

if __name__ == '__main__':
   MAPIProfile = ""
   SendTo = "python-win32 at python.org"
   SendMessage = "testing one two three"
   SendSubject = "Testing Extended MAPI!!"
   SendEMAPIMail(SendSubject, SendMessage, SendTo, MAPIProfile=MAPIProfile)



More information about the Python-win32 mailing list