简体   繁体   中英

win32com.client.gencache.EnsureDispatch(“Outlook.Application”) not working

I am trying to send email using my Outlook client through a Python script. When the below Python script is executed separately from IDLE editor, it is working fine. But when the same lines of codes are put at the end of another Python script (so that after the logic execution email is triggered) and the Python script is called from a Web application hosted in Tomcat server, it is not working.

Code Snippet:

#!/Python36/python.exe -u

import win32com.client`

mail_subject = "Test Mail Subject"

mail_body    = "Test Mail Body"

Outlook      = Win32com.client.gencache.EnsureDispatch("Outlook.Application")
mail         = Outlook.CreateItem(0)
mail.To      = 'xxx@abc.com'
mail.Subject = mail_subject
mail.Body    = mail_body
mail.Send()

To check where it is failing, I had printed some comments after each line. It was found that the line:
Outlook = win32com.client.gencache.EnsureDispatch("Outlook.Application")

was failing when the Python script was executed from the Web application. Can anyone help with the resolution.

According to your question description of the problem, you can use try the following two ways alternatively:

1.Please include the code below before win32com.Client.Gencache.EnsureDispatch("Outlook.Application")

from win32com.client.gencache import EnsureDispatch

2.You should use the following code replace the code The win32com. Client.Gencache.EnsureDispatch("Outlook.Application") win32com. Client.Gencache.EnsureDispatch("Outlook.Application")

Gencache. EnsureDispatch ("Outlook.Application")

Please refer to the website below for details: https://www.programcreek.com/python/example/103552/win32com.client.gencache.EnsureDispatch

If you have any question, please reply to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM