Search code examples
pythonoutlookwin32com

Reply to an Outlook email using the MessageID and Win32com module in Python


So I know how to look through the inbox (or any other folder) and find emails to reply to. However in my case, I have a .msg email file from which I extract the MessageID, and I'm looking to use win32com module to reply to that specific email.

Basically I'm looking for something like this:

from extract_msg import Message
msg = Message("message.msg")
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0x0)

mail.To = "; ".join(to)
mail.Subject = subject
mail.Body = body
mail.InReplyTo = msg.messageId

I understand that something similar is doable using the smtplib module using:

message['In-Reply-To'] = msg.messageId

but I cannot get smtplb to work with Outlook. And thus, I'm using win32com.


Solution

  • The PR_IN_REPLY_TO_ID property should be set to the PR_INTERNET_MESSAGE_ID property value. Make sure that such value exists in Outlook messages. You can get the value in Outlook in the following way (the sample is in C# but the Outlook object model is common for all kind of programming languages):

    string PR_INTERNET_MESSAGE_ID = "http://schemas.microsoft.com/mapi/proptag/0x1035001F";
    Microsoft.Office.Interop.Outlook.PropertyAccessor pal = mailItem.PropertyAccessor;
    string Internet_Message_Id = pal.GetProperty(PR_INTERNET_MESSAGE_ID).ToString();