定义要将电子邮件保存到的邮箱win32client python

2024-05-15 12:28:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用win32 API for Outlook将电子邮件保存到共享邮箱的草稿文件夹。我可以将电子邮件保存到我的(默认?)邮箱草稿文件夹使用以下选项:

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')

多亏了this previous question,我可以看到SendUsingAccount()方法可以用于从定义的邮箱发送邮件。是否有一种等效的方法将保存到已定义邮箱的草稿文件夹中?你知道吗


Tags: 方法text文件夹apifor定义电子邮件mail
1条回答
网友
1楼 · 发布于 2024-05-15 12:28:55

当您将帐户切换为发送电子邮件时,可以选择Save (),电子邮件将保存在新帐户的草稿框中。你知道吗

代码:

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不起作用。返回值为“无”。始终从第一个电子邮件帐户发送邮件。您需要调用oleobj_.Invoke()的方法。你知道吗

更新:

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

类似情况:python win32com outlook 2013 SendUsingAccount return exception

相关问题 更多 >