Reputation: 8927
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 = '[email protected]'
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?
Upvotes: 1
Views: 8339
Reputation: 66235
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 (I am its author) and its RDOSession.Accounts.AddPOP3Account method.
Upvotes: 2
Reputation: 496
I know this comes very late but this is how I've managed to select a specific e-mail address to send the e-mail from. The address needs to be among your outlook addresses.
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.Subject = "Test subject"
mail.To = "[email protected]"
# 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 "iongroup.com" in str(myEmailAddress):
From = myEmailAddress
break
if From != None:
# This line basically calls the "mail.SendUsingAccount = [email protected]" outlook VBA command
mail._oleobj_.Invoke(*(64209, 0, 8, 0, From))
mail.Send()
Upvotes: 4
Reputation: 49395
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:
Upvotes: 2