Home > front end >  How do I move files from my inbox into Online Archive mailbox using Python?
How do I move files from my inbox into Online Archive mailbox using Python?

Time:05-24

SOLVED ! :) I used the following to move my mail from somewhere in my inbox into online archives with some important help mentioned below :

import win32com
import os
import sys

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
src = mapi.GetDefaultFolder(6).Folders["tobemoved"]
target = mapi.Folders["Online Archive - XXX"].Folders['Archive']

messages = src.Items


i = range(messages.count, 1, -1)
for x in i:
     print(x)
     messages(x).Move(target)
`

I have additional folder called 'Online-Archive-Same email address as "inbox" email ' that i currently can't locate it tried to use this link to figure out the enumeration of it . but no luck ..

as i must free up some disk space ill appreciate any help given. P.S tried the conventional way - with outlook struggling with connection issues and 22k email to be moved to be archived outlook just giving up on me :) feel free to advise anything that can resolve this issue.

CodePudding user response:

You can access the Office 365 Online Archive folders like this:

Replace the example email with the exact email address you see in outlook.


import win32com.client
import win32com

app = win32com.client.gencache.EnsureDispatch("Outlook.Application")
outlook = app.GetNamespace("MAPI")
outlook_folder = outlook.Folders['Online Archive - [email protected]'].Folders['Inbox']

item_count = outlook_folder.Items.Count
print(item_count)


180923

CodePudding user response:

On the low (Extended MAPI) level (C or Delphi only), Online Archive is just another delegate Exchange mailbox. The only way to distinguish an archive mailbox from yet another delegate mailbox owned by some Exchange user is by reading PR_PROFILE_ALTERNATE_STORE_TYPE property in the archive store profile section - retrieve the store entry id (PR_ENTRYID), then find the matching row in the session stores table (IMAPISession::GetMsgStoresTable). For the matching row (use IMAPISession::CompareEntryIDs), retrieve PR_PROVIDER_UID property. Use its value to call IMAPISession.OpenProfileSection. Read PR_PROFILE_ALTERNATE_STORE_TYPE property from the IProfSect object and check if its value is "Archive" (unlike the store name, is not localized).

If Extended MAPI in C or Delphi is not an option, you can either

  1. Try to find a matching store in the Namespace.Stores collection with the name starting with "Online Archive - " and the SMTP address of the user. Since that prefix is locale specific, that is not something I would use in production code.

  2. Use Redemption (I am its author) - it exposes RDOExchangeMailboxStore.IsArchive property. If the archive store is not already opened in Outlook, you can also use RDOSession.GetArchiveMailbox. In VB script:

set rSession = CreateObject("Redemption.RDOSession")
rSession.MAPIOBJECT = Application.Session.MAPIOBJECT
userAddress =  rSession.CurrentUser.SMTPAddress
set store = GetOpenArchiveMailboxForUser(userAddress)
if not store is Nothing Then
  MsgBox "Found archive store for " & userAddress
Else
  MsgBox "Could not find archive store for " & userAddress
End If

function GetOpenArchiveMailboxForUser(SmtpAddress)
  set GetOpenArchiveMailboxForUser = Nothing
  for each store in rSession.Stores
    if TypeName(store) = "RDOExchangeMailboxStore" Then
      Debug.Print store.Name & " - " & store.Owner.SMTPAddress & " - " &  store.IsArchive
      if store.IsArchive and LCase(store.Owner.SMTPAddress) = LCase(SmtpAddress) Then
        set GetOpenArchiveMailboxForUser = store
        exit for
      End If
    End If
  next

end function

  • Related