需要使用Python在Outlook中切换帐户以使用其他帐户发送电子邮件

时间:2018-09-06 11:15:18

标签: python outlook outlook-redemption

我有以下代码

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'madanraj.c@sss.com;
mail.Subject = 'Daily Backlog'
mail.Send()

我在Outlook中有两个帐户,并且我也安装了兑换功能,但遇到了如何切换帐户的问题

我看到了vb代码,但无法转换为python代码

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Accounts = Session.Accounts
for each Account in Accounts
  Debug.Print Account.Name
next

2 个答案:

答案 0 :(得分:1)

设置MailItem.SendUsingAccount属性。

答案 1 :(得分:0)

我知道来晚了,但我认为人们可能会发现它很有用。这是我设法选择要发送的电子邮件地址的方式,因为我在Outlook中有多个地址。

import win32com.client as win32

outlook = win32.Dispatch('outlook.application') 

mail = outlook.CreateItem(0)
mail.Subject = "Test subject"
mail.To = "yourrecipient@gmail.com"

# If you want to set which address the e-mail is sent from. 
# The e-mail needs to be part of your outlook account.
From = None
for myEmailAddress in outlook.Session.Accounts:
    if "gmail.com" in str(myEmailAddress):
        From = myEmailAddress
        break

if From != None:
    # This line basically calls the "mail.SendUsingAccount = xyz@email.com" outlook VBA command
    mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))

mail.Send()