【发布时间】:2021-02-24 01:23:57
【问题描述】:
该脚本可以发送电子邮件,但是如果我不保持 Outlook 打开,电子邮件就会卡在 Outlook 2015 的发件箱中。即使我打开它也需要 4-5 分钟才能最终发送。这里的问题是我希望它作为远程桌面上的计划任务,因此我不会登录到机器上的 Outlook。问题仍然存在,带或不带附件。
import win32com.client
import time
from win32com.client import Dispatch, constants
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "subject here"
newMail.BodyFormat = 2
cnt = cnt
SENDER = "email"
RECIPIENTS = ["email list"]
newMail.HTMLBody = """\
<html>
<head></head>
<body>
<p>There are {cnt} new repricing files uploaded. <br>
<br>
~Sent via Python Code
</p>
</body>
</html>
""".format(cnt=cnt)
attachment = "file.xlsx"
newMail.Attachments.Add(attachment)
newMail.To = "; ".join(RECIPIENTS)
newMail.display()
time.sleep(5)
newMail.Send()
在调用newMail.send() 后,Outlook 会自动关闭,这很好,但这会导致电子邮件卡在发件箱中。
【问题讨论】:
-
正如你所说,我只能通过注释掉
newMail.display()行来解决它。