我一直在尝试下面的代码。
def send_mail():
file_path = r""
outlook_app = win32.Dispatch('Outlook.Application')
olNS = outlook_app.GetNameSpace('MAPI')
# choose sender account
send_account = None
for account in outlook_app.Session.Accounts:
if account.DisplayName == 'sender@mail.com':
# if account.SmtpAddress == "sender@mail.com":
send_account = account
break
mail_item = outlook_app.CreateItem(0) # 0: olMailItem
mail_item._oleobj_.Invoke(*(64209, 0, 8, 0, send_account))
#mail_item._oleobj_.Invoke(*(64209,0,8,0,olNS.Accounts.Item("sender@email.com")))
mail_item.display()
mail_item.TO = 'sender@mail.com'
# mail_item.CC = 'sender@mail.com'
# mail_item.Recipients.Add('sender@mail.com')
mail_item.Subject = 'Test sending using particular account'
mail_item.BodyFormat = 1 # 1: Plain format
mail_item.Attachments.Add(file_path)
mail_item.Body = '''Hello, This is a test mail.\nThanks Guys.
'''
mail_item.Send()
即使在设置 send_account 之后,它也会选择我组织的邮件 ID,而不是另一个。我也使用过 SendUsingAccount,但得到了相同的结果。