Home > Enterprise >  Python `mail.Send` successful but outlook email not delivered
Python `mail.Send` successful but outlook email not delivered

Time:03-31

I already referred this 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 == '[email protected]':
        print("hi")
        mail.SendUsingAccount = acc.DisplayName
mail.To = '[email protected]'
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()

CodePudding user response:

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 == '[email protected]':
        print("hi")
        mail.SendUsingAccount = acc.DisplayName
mail.To = '[email protected]'
mail.Subject = 'Test Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.Send()
  • Related