0

I already referred this post. Don't mark it as duplicate please.

I wrote the below code to send an email via python

outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
From = outlook.Session.Accounts[1]
mail.To = 'test@org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))
mail.Send()  # successfully executed

The above code is successfully executed but still the email is not delivered and its been more than 15 mins. If I do it manually using outlook,am able to send and receive messages.

Can help me what is the issue here?

update - mail.display() looks like below

enter image description here

update - mail.Send() in outbox looks like below

enter image description here

update - error for 2nd mailbox

enter image description here

update - code

mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
    if acc.DisplayName == 'user2@org.com':
        print("hi")
        mail.SendUsingAccount = acc.DisplayName
mail.To = 'user1@org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail._oleobj_.Invoke(*(64209, 0, 8, 0, mail.SendUsingAccount))
pythoncom.CoInitialize()
The Great
  • 7,215
  • 7
  • 40
  • 128

1 Answers1

1

The difference between Outlook and your code is the synchronization with the mail server. Outlook may cache submitted items and send them when the store is synced with the mail server.

The NameSpace.SendAndReceive method initiates immediate delivery of all undelivered messages submitted in the current session, and immediate receipt of mail for all accounts in the current profile. SendAndReceive provides the programmatic equivalent to the Send/Receive All command that is available when you click Tools and then Send/Receive. All accounts defined in the current profile are used in Send/Receive All. If an online connection is required to perform the Send/Receive All, the connection is made according to user preferences.

Read more about that in the How To: Perform Send/Receive in Outlook programmatically article.

Also you may try to run the following code:

mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
    if acc.DisplayName == 'user2@org.com':
        print("hi")
        mail.SendUsingAccount = acc.DisplayName
mail.To = 'user1@org.com'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.Send()
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • upvoted. But when i do click "send and receive" on outlook, it just doesn't synchronize and throws error as shown above. – The Great Mar 27 '22 at 23:39
  • Have you tried to remove the `mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))` line of code? Does it work correctly in that case? – Eugene Astafiev Mar 28 '22 at 07:22
  • If you don't mind, can I seek your help to write the code in your answer please? by copying my code and also including the changes that you suggested? because, when I try, it doesn't work. So, want to make sure whether am doing it correctly. would really be grateful – The Great Mar 28 '22 at 08:02
  • doing send and receive programmatically doesn't contain python (and am not sure how to do that). help please – The Great Mar 28 '22 at 08:04
  • C++ can use WinSock to implement this – Junjie Zhu - MSFT Mar 28 '22 at 09:55
  • WinSock is not related to the original task in any way. – Eugene Astafiev Mar 28 '22 at 10:07