我正在尝试使用Python在Outlook中发送电子邮件,但遇到了错误。我不确定问题的原因。可能是与服务器有关,但错误似乎表明是脚本出了问题。电子邮件脚本如下:
import win32com.client as win32
import psutil
import os
import subprocess
def send_notification():
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'me@mycompany.com',
mail.Subject = 'Sent through Python'
mail.body = 'This email alert is auto generated. Please do not respond.'
mail.send
# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below
def open_outlook():
try:
subprocess.call(['C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe'])
os.system("C:\Program Files (x86)\Microsoft Office\Office14\Outlook.exe");
except:
print("Outlook didn't open successfully")
# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
p = psutil.Process(item)
if p.name() == "OUTLOOK.EXE":
flag = 1
break
else:
flag = 0
if (flag == 1):
send_notification()
else:
open_outlook()
send_notification()
我收到的错误信息如下:
"File "C:\Users***\Desktop\CORE\Query.py", line 78, in send_notification()
File "C:\Users****\Desktop\CORE\Query.py", line 53, in send_notification mail.To = '@.com',
File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 565, in setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)"
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, u'Microsoft Outlook', u'The object does not support this method.', None, 0, -2147352567), None)"
如果有人能提供一些关于如何让脚本正常运行的建议,我将不胜感激。
谢谢!
MailItem.To
接受一个字符串列表而不是单个字符串。尝试将其更改为mail.To = ['me@mycompany.com']
。此外,您在该行之后有一个逗号,这可能不是您想要的。 - Garrett Gutierrezmail.To = 'me@mycompany.com'
通常可以正常工作。请查看此问题。 - Ben.T