Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically send outlook email from shared mailbox

I'm trying to send an email with python from a shared mailbox.

I have been able to sucessfuly send it through my own email, but sending one with a shared mailbox (that I have tested that I have access too) is giving me issues.

Code used for email script in python

import win32com.client
import win32com
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Python Email Test"
newMail.Body = "Test"
newMail.To = '[email protected]'
newMail.Send()

I know that below is how I can read my emails from a shared Folder. outlook = win32com.Dispatch("Outlook.Application").GetNamespace("MAPI") dir_accounts = outlook.Folders("SharedFolder")

Any ideas on how to combine these?

like image 397
Gil5Ryan Avatar asked Aug 10 '16 14:08

Gil5Ryan


People also ask

How do I send email from shared mailbox not on behalf of Outlook?

Outlook Desktop App (Windows)When composing a message click on the From in the drop-down box and select Other E-Mail Address. Type in the email address here that you wish to Send As and click OK. The next time you compose a message the Alternate Email Address will be available from the From Drop Down Box.


2 Answers

In case if you have multiple accounts configured in Outlook you may use the SendUsingAccount property of the MailItem class. Or if you have sufficient privileges (rights) you may consider using the SentOnBehalfOfName property which is a string indicating the display name for the intended sender of the mail message.

like image 136
Eugene Astafiev Avatar answered Nov 14 '22 23:11

Eugene Astafiev


Added this right before the newMail.send() step and it worked

newMail.SentOnBehalfOfName = 'SharedFolder'
like image 37
Gil5Ryan Avatar answered Nov 14 '22 23:11

Gil5Ryan