0

I am having trouble adding new MailItems to a PST file that I create dynamically. I can create the MailItems fine on their own, and saving them to a location on my computer works just fine. When I try to save the MailItems to the PST, however, nothing happens. I have tried to call the Items.Add() method, it gives me a new MailItem, but the Items collection is not increased. I have searched everywhere for a solution, I have seen some examples in VBA and C#, but since I have no experience with either of the languages, translating to Python is not working out well.

Currently using Python 3.9.1

Below is was I am doing, please excuse any mistakes, I'm extremely rusty on my Python:

Outlook = None
pst = os.path.join(self.destPath, (dest + ".pst")

try:
    Outlook = win32com.client.Dispatch(r'Outlook.Application')
except pywintypes.com_error as ex:
...

if Outlook:
    ns = Outlook.getNamespace(r'MAPI')
    ns.AddStore(pst)
    pststore = None
    
    for store in ns.Stores:
        if store.GetRootFolder().Name == dest:
            pststore = store
            break

    ... #get mailItem information from external EML .....

    pstfld = pststore.GetRootFolder().Folders.Add("Sent")
    
    folderItems = pstfld.Items
    mailItem = folderItems.Add("IPM.Note")
    mailItem.Subject = ....
    mailItem.Recipients.Add(...)
    mailItem.Sender = ...
    mailItem.HTMLBody = ...

    mailItem.Save()

    .....

    ns.RemoveStore(pststore.GetRootFolder())

Thanks in advance!

EDIT:

Figured out a temporary work-around after posting the question, and messing with the code some more. Calling mailItem.Move(pstfld) after mailItem.Save() gave me the results I needed; the MSG files are added to the PST file.

mdershti
  • 3
  • 3

0 Answers0