-1

I am on a business project with python, where my team will send out an automated report in some teams. The code for sending it out works quite well:

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional

# To attach a file to the email (optional):
attachment  = "Path to the attachment"
mail.Attachments.Add(attachment)

mail.Send()

Thanks to this thread: Send Outlook Email Via Python?

Is there anyway to change the adress of the sender, something like:

mail.From = 'Team Adress'

Otherwise the problem will be that people will answer the mail to my e-mail adress, which will be so wrong approach. Or is that not possible at all, because It has to open my outlook account?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
PV8
  • 5,799
  • 7
  • 43
  • 87

1 Answers1

2

There are two possible ways to specify the sender:

  1. If you have multiple accounts configured in Outlook you may use the MailItem.SendUsingAccount property which allows setting an Account object that represents the account under which the MailItem is to be sent. For example:
        public static void SendEmailFromAccount(Outlook.Application application, string subject, string body, string to, string smtpAddress) 
        { 

            // Create a new MailItem and set the To, Subject, and Body properties. 
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem); 
            newMail.To = to; 
            newMail.Subject = subject; 
            newMail.Body = body; 

            // Retrieve the account that has the specific SMTP address. 
            Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress); 
            // Use this account to send the email. 
            newMail.SendUsingAccount = account; 
            newMail.Send(); 
        } 


        public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress) 
        { 

            // Loop over the Accounts collection of the current Outlook session. 
            Outlook.Accounts accounts = application.Session.Accounts; 
            foreach (Outlook.Account account in accounts) 
            { 
                // When the email address matches, return the account. 
                if (account.SmtpAddress == smtpAddress) 
                { 
                    return account; 
                } 
            } 
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
        } 
  1. The MailItem.SentOnBehalfOfName property allows setting a string indicating the display name for the intended sender of the mail message. Be aware, you need to have permission to be able to send anything on behalf of another person.
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • How would I include it in my code, the first part? The second part ends up with: `mail.SentOnBehalfOfName = 'Sender' `, which will be shown as: Sender in behalf of: – PV8 Oct 11 '19 at 11:59