def parse_msg_file(filepath):
    mapi.MAPIInitialize((mapi.MAPI_INIT_VERSION, 0))
    storage_flags = STGM_DIRECT | STGM_READ | STGM_SHARE_EXCLUSIVE
    storage = pythoncom.StgOpenStorage(filepath, None, storage_flags, None, 0)
    mapi_session = mapi.OpenIMsgSession()
    message = mapi.OpenIMsgOnIStg(mapi_session, None, storage, None, 0,
                                  mapi.MAPI_UNICODE)
    return message
Beispiel #2
0
    def mapi_initialize (self):
        logging.info('Initalizing MAPI...')
        mapi.MAPIInitialize(None)
        logging.info('Initalizing MAPI...done')
        flags = (mapi.MAPI_EXTENDED | mapi.MAPI_USE_DEFAULT | MOD_FLAG)

        logging.info('Opening default profile in MAPI...')
        self.set_olsession(mapi.MAPILogonEx(0, "", None, flags))
        logging.info('Opening default profile in MAPI...done')
Beispiel #3
0
 def __init__(self, read_only=False):
     old_cwd = os.getcwd()
     mapi.MAPIInitialize(None)
     logonFlags = (mapi.MAPI_NO_MAIL | mapi.MAPI_EXTENDED
                   | mapi.MAPI_USE_DEFAULT)
     self.session = mapi.MAPILogonEx(0, None, None, logonFlags)
     if read_only:
         self.mapi_flags = mapi.MAPI_DEFERRED_ERRORS
     else:
         self.mapi_flags = mapi.MAPI_DEFERRED_ERRORS | mapi.MAPI_BEST_ACCESS
     self.outlook = None
     os.chdir(old_cwd)
Beispiel #4
0
    def __init__(self):
        logging.info('Logging in...')

        # initialize and log on
        mapi.MAPIInitialize(None)
        flags = mapi.MAPI_EXTENDED | mapi.MAPI_USE_DEFAULT | MOD_FLAG
        self.session = mapi.MAPILogonEx(0, "", None, flags)

        self.def_msgstore = self.def_inbox_id = self.def_inbox = None
        self.def_cf = None

        self.def_msgstore = self.get_default_msgstore()
        self.def_inbox_id = self.get_default_inbox_id()
        self.def_inbox = self.get_default_inbox()
        self.def_cf = self.get_default_cf()
Beispiel #5
0
    def open(self):
        """ 
		Ensures we have a MAPI Session to the exchange server. Returns whether or 
		not a new connection was required (True or False). 
		"""
        if self.connection:
            # Nothing to do if the connection is already open.
            return False
        try:
            mapi.MAPIInitialize(None)
            if self.MAPIProfile <> None:
                MAPIProfile = self.MAPIProfile
            else:
                MAPIProfile = ""
            session = mapi.MAPILogonEx(
                0, MAPIProfile, None,
                mapi.MAPI_EXTENDED | mapi.MAPI_USE_DEFAULT)
            messagestorestable = session.GetMsgStoresTable(0)
            messagestorestable.SetColumns(
                (mapitags.PR_ENTRYID, mapitags.PR_DISPLAY_NAME_A,
                 mapitags.PR_DEFAULT_STORE), 0)
            while True:
                rows = messagestorestable.QueryRows(1, 0)
                #if this is the last row then stop
                if len(rows) != 1:
                    break
                row = rows[0]
                #if this is the default store then stop
                if ((mapitags.PR_DEFAULT_STORE, True) in row):
                    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]
            #check for errors
            if mapitags.PROP_TYPE(tag) == mapitags.PT_ERROR:
                raise TypeError('got PT_ERROR instead of PT_BINARY: %s' % eid)
            self.connection = msgstore.OpenEntry(eid, None,
                                                 mapi.MAPI_BEST_ACCESS)
            return True
        except:
            if not self.fail_silently:
                raise
def SendEMAPIMail(Subject="",
                  Message="",
                  SendTo=None,
                  SendCC=None,
                  SendBCC=None,
                  MAPIProfile=None):
    """Sends an email to the recipient using the extended MAPI interface
    Subject and Message are strings
    Send{To,CC,BCC} are comma-separated address lists
    MAPIProfile is the name of the MAPI profile"""

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

    while True:
        rows = messagestorestable.QueryRows(1, 0)
        #if this is the last row then stop
        if len(rows) != 1:
            break
        row = rows[0]
        #if this is the default store then stop
        if ((mapitags.PR_DEFAULT_STORE, True) in row):
            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]
    #check for errors
    if mapitags.PROP_TYPE(tag) == mapitags.PT_ERROR:
        raise TypeError('got PT_ERROR instead of PT_BINARY: %s' % eid)
    outboxfolder = msgstore.OpenEntry(eid, None, mapi.MAPI_BEST_ACCESS)

    # create the message and the addrlist
    message = outboxfolder.CreateMessage(None, 0)
    # note: you can use the resolveaddress functions for this. but you may get headaches
    pal = []

    def makeentry(recipient, recipienttype):
        return ((mapitags.PR_RECIPIENT_TYPE,
                 recipienttype), (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)
from win32com.client import Dispatch
from win32com.mapi import mapi
from win32com.mapi.mapitags import *

mapi.MAPIInitialize(None)
logonFlags = mapi.MAPI_NO_MAIL | mapi.MAPI_EXTENDED
session = mapi.MAPILogonEx(0, None, None, logonFlags)

MAPI_SUBSYSTEM = 39

restriction = mapi.RES_PROPERTY, (mapi.RELOP_EQ, PR_RESOURCE_TYPE,
                                  (PR_RESOURCE_TYPE, MAPI_SUBSYSTEM))
table = session.GetStatusTable(0)
rows = mapi.HrQueryAllRows(
    table,
    (PR_DISPLAY_NAME_A, ),  # columns to retrieve
    restriction,  # only these rows
    None,  # any sort order is fine
    0)  # any # of results is fine
assert len(rows) == 1, "Should be exactly one row"
(tag, val), = rows[0]
print "Profile name:", val