Ken
Ken

Reputation: 61

Cannot switch sender email when using ReplyAll via Outlook in Python Win32

I have multiple account associated to my outlook, i am trying to set the From field to this one specific email i own. Looking at https://learn.microsoft.com/en-gb/office/vba/api/outlook.mailitem I should be able to accomplish this by changing the SendUsingAccount property, however, i am running into ERROR:root:(-2147352571, 'Type mismatch.', None, 1). Does anyone know why?

pythoncom.CoInitialize()
outlook = win32.Dispatch("Outlook.Application")
selection = outlook.ActiveExplorer().Selection
count = selection.Count + 1
for i in range(1, count):
    message = selection.Item(i)
    reply = message.ReplyAll()
    newBody = "test"
    for myEmailAddress in outlook.Session.Accounts:
       if "@test.com" in str(myEmailAddress):
           From = myEmailAddress
           break
           print(From.DisplayName) #prints the email i want fine
    reply.SendUsingAccount = From.DisplayName #this line is giving me the error. If I remove it , the email popups fine, but the From address is defaulting to one i dont want to use
    reply.HTMLBody = newBody + reply.HTMLBody
    reply.Display(False)

Upvotes: 0

Views: 173

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Application.Session.Accounts collection returns Account objects, not strings. And MailItem.SendUsingAccount property takes an Account object, not a string.

Replace the line

if "@test.com" in str(myEmailAddress):

with

if "@test.com" in str(myEmailAddress.SmtpAddress):

and

Reply.SendUsingAccount = From.DisplayName

with

Reply.SendUsingAccount = From

Upvotes: 1

Related Questions