Send Email From Specific Outlook Account Via Python?

≡放荡痞女 提交于 2019-12-06 14:01:20

问题


I had some code that was written to email lab users whenever certain processes had finished running. This was sent from a gmail account, using SMTP.

However, my supervisor wants the mail to be sent from an official department address, which means that I have use Outlook and MAPI. I've had an account created which I want the email to originate from regardless of the lab machine the job is being run on. The problem is that I can only get email to send from the local Outlook account, and not all of the lab machines have a local account.

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'user@domain.com'
mail.Subject = 'Message Subject'
mail.body = 'Message text. Message text'
mail.send

Surely there's a way to specify the username/password/server that I want the email to be sent from?


回答1:


The Outlook object model doesn't provide anything for configuring profiles. However, if you have an account configured in Outlook you may find the SendUsingAccount property of the MailItem class helpful. It allows to set an Account object that represents the account under which the MailItem is to be sent.

You may find the following links helpful:

  • Configure Outlook Mail Settings Programmatically?
  • White paper: Configuring Outlook profiles by using a PRF file



回答2:


As Eugene suggested, you can either manually create a POP3/SMTP account and assign it to the MailItem.SendUsingAccount property prior to calling Send, or you can create a new POP3/SMTP account dynamically using Redemption and its RDOSession.Accounts.AddPOP3Account method.



来源:https://stackoverflow.com/questions/30243518/send-email-from-specific-outlook-account-via-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!