簡體   English   中英

定義要保存 email 的郵箱 - win32client python

[英]Define mailbox to which to save an email - win32client python

我想使用 Outlook 的 win32 API 將 email 保存到共享郵箱的草稿文件夾中。 我可以使用以下命令將 email 保存到我的(默認?)郵箱草稿文件夾中:

def TestEmailer(text, subject, recipient):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.HtmlBody = text
    mail.Save()

TestEmailer('hello world', 'test', 'recipient@gmail.com')

感謝前面的問題,我可以看到SendUsingAccount()方法可用於從定義的郵箱發送。 是否有一種等效的方法可以保存到已定義郵箱的草稿文件夾中?

切換賬號時可以select Save ()發送email,會保存在新賬號的草稿箱中。

代碼:

import win32com.client as win32

def send_mail():
    outlook_app = win32.Dispatch('Outlook.Application')

    # choose sender account
    send_account = None
    for account in outlook_app.Session.Accounts:
        if account.DisplayName == 'sender@hotmail.com':
            send_account = account
            break

    mail_item = outlook_app.CreateItem(0)   # 0: olMailItem

    # mail_item.SendUsingAccount = send_account not working
    # the following statement performs the function instead
    mail_item._oleobj_.Invoke(*(64209, 0, 8, 0, send_account))

    mail_item.Recipients.Add('receipient@outlook.com')
    mail_item.Subject = 'Test sending using particular account'
    mail_item.BodyFormat = 2   # 2: Html format
    mail_item.HTMLBody = '''
        <H2>Hello, This is a test mail.</H2>
        Hello Guys. 
        '''

    mail_item.Save()


if __name__ == '__main__':
    send_mail()

這里有一點黑魔法。 直接設置mail_item.SendUsingAccount行不通的。 返回值為無。 始終從第一個 email 帳戶發送郵件。 您需要調用oleobj_.Invoke()的方法。

更新:

Oleobj 文檔: https://github.com/decalage2/oletools/wiki/oleobj

類似案例: python win32com outlook 2013 SendUsingAccount 返回異常

暫無
暫無

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

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