簡體   English   中英

通過 python 腳本發送時,Email 卡在 outlook 發件箱中

[英]Email gets stuck in outlook outbox when sending via python script

該腳本可以發送電子郵件,但是如果我不保留 outlook 打開 email 會卡在 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 會自動關閉,這很好,但這會導致 email 卡在發件箱中。

您可以使用Application.Session.SendAndReceive方法。 請記住,該過程是異步的。 如果您需要等待發送/接收完成,您可以使用SyncObject.SyncEnd事件。

正如你所說,我只能通過注釋掉newMail.display()行來解決它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM