Python: 使用其他邮箱发送邮件

Python: Using another mailbox in sending emails

我的 Outlook 上有三个邮箱。如何使用我的其他邮箱通过 Python 发送电子邮件?下面的脚本目前使用我的主收件箱 = jgliban@ims.com。如何使用我的其他邮箱 (jgliban@iqvia.com) 发送脚本上的电子邮件?

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.GetInspector
mail.To = 'jgliban@ims.com'
mail.CC = 'jgliban@ims.com'
mail.Subject = 'Test Email'
index = mail.HTMLbody.find('>', mail.HTMLbody.find('<body')) 
mail.HTMLbody = mail.HTMLbody[:index + 1] + 'Hello,<br> <br>This is a test email<br><br>Many thanks and kind regards. ' + mail.HTMLbody[index + 1:]
mail.Send()

我正要建议使用 SMTP,但由于您已经使用 win32 连接到 Outlook 应用程序,也许这会有所帮助?

Need to switch accounts in outlook using python for sending email using other account

来自link中的回答:

Set MailItem.SendUsingAccount property.

编辑:嗯。评论中给出:

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
for acc in outlook.Session.Accounts:
    print (acc)
    if str(acc) == 'jgliban@iqvia.com':
        mail.SendUsingAccount = acc

如果是 Exchange 邮箱,请将 MailItem.SentOnBehalfOfName 属性 设置为代理邮箱的名称。