【问题标题】:Email gets stuck in outlook outbox when sending via python script通过 python 脚本发送时,电子邮件卡在 Outlook 发件箱中
【发布时间】: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() 行来解决它。

标签: python outlook win32com


【解决方案1】:

您可以使用Application.Session.SendAndReceive 方法。请记住,该过程是异步的。如果您需要等待发送/接收完成,您可以使用SyncObject.SyncEnd 事件。

【讨论】:

  • 您能否提供更多有关我如何将其合并到上述代码中的上下文?我尝试将Session.SendAndRecieve 调用到newMail object,但出现属性错误。谢谢!
  • 啊,其实我意识到了这个问题。当我删除 newMail.display() 它工作正常。结合这两个调用时一定是一个错误。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
相关资源
最近更新 更多