【发布时间】:2018-08-20 15:27:36
【问题描述】:
我正在尝试使用 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()
我收到的错误消息是:
“文件“C:\Users***\Desktop\CORE\Query.py”,第 78 行,在 发送通知()
文件“C:\Users****\Desktop\CORE\Query.py”,第 53 行,在 send_notification 中 mail.To = '@.com',
文件“C:\Python27\lib\site-packages\win32com\client\dynamic.py”,第 565 行,在 setattr self.oleobj.Invoke(entry.dispid, 0, invoke_type, 0, value)"
pywintypes.com_error: (-2147352567, '发生异常。', (4096, u'Microsoft Outlook', u'对象不支持此方法。', None, 0, -2147352567), None)"
如果有人可以就我可以做些什么来使脚本正常工作提供一些建议,我将不胜感激。
谢谢!
【问题讨论】:
-
不能确定这是问题的原因,所以我不会回答,但我相信
MailItem.To需要一个字符串列表而不是单个字符串。尝试将其设为mail.To = ['me@mycompany.com']。此外,您在该行后面还有一个逗号,可能不应该在那里。 -
正如@GarrettGutierrez 所说,逗号可能是错误,只写
mail.To = 'me@mycompany.com'可以正常工作。去看看this question -
是的,是逗号。感谢您的帮助!
-
发送是一个方法:mail.send()