Home > Blockchain >  Python outlook sent items folder data extraction( using MAPI and pywin32)
Python outlook sent items folder data extraction( using MAPI and pywin32)

Time:11-18

Here I am trying to extract one particular email data which is present in sent items folder in outlook? but I am unable to find any method to give such condition , like if this particular email present in sent item folder then extract subject, date & time.

import win32com.client
from win32com.client import Dispatch

import  regex as re
from datetime import datetime, timedelta

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")

sentitem = mapi.GetDefaultFolder(5)
messages = sentitem.Items


for msg in messages:

here after I am not getting any idea. Can anyone help me to find this

CodePudding user response:

You cannot use regex with the Outlook Object Model, but you can use Items.Find/FindNext and Items.Restrict to find the message(s).

See https://docs.microsoft.com/en-us/office/vba/api/outlook.items.restrict for the syntax and sample queries.

CodePudding user response:

Iterating over all items and checking item's properties is not really a good idea when dealing with the Outlook object model:

for msg in messages

Instead, the OOM provides three main ways for getting the job done:

  1. Use the Find/FindNext methods of the Items class. See How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET) for more information.

  2. Use the Restrict method of the Items class. This method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found. See How To: Use Restrict method to retrieve Outlook mail items from a folder for more information.

  3. The AdvancedSearch method of the Application class. The key benefits of using the AdvancedSearch method in Outlook are:

    • The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
    • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
    • Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
    • You can stop the search process at any moment using the Stop method of the Search class.

    Read more about the AdvancedSearch method in the Advanced search in Outlook programmatically: C#, VB.NET article.

  • Related