简体   繁体   English

win32com.client.gencache.EnsureDispatch(“ Outlook.Application”)不起作用

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

I am trying to send email using my Outlook client through a Python script. 我试图通过Python脚本使用Outlook客户端发送电子邮件。 When the below Python script is executed separately from IDLE editor, it is working fine. 当下面的Python脚本与IDLE编辑器分开执行时,它可以正常工作。 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. 但是,当将相同的代码行放在另一个Python脚本的末尾(以便在触发逻辑执行电子邮件之后),并且从Tomcat服务器中托管的Web应用程序调用Python脚本时,它将无法正常工作。

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. 从Web应用程序执行Python脚本时失败。 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") 1.请在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") 2.您应使用以下代码替换代码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 有关详细信息,请访问下面的网站: https : //www.programcreek.com/python/example/103552/win32com.client.gencache.EnsureDispatch

If you have any question, please reply to me. 如有任何疑问,请回复我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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