3

I am trying to use the win32 Python library to send some emails, but I am unable to specify the sensitivity label in an automatic way. As soon as I try to send out the email by using the below code, the Azure Information Protection dialog box pops up making this impossible to automate.

As I understood from reading various articles, there might be a workaround specifying the GUID string, but I have no idea how to do it.

I would really appreciate if you could help me out!

outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'emailaddress@email.com'
mail.Subject = 'Sample Email'
mail.HTMLBody = '<h3>This is HTML Body</h3>'
mail.Body = "This is the normal body"
mail.Sensitivity = 3
mail.Send()
DaniB
  • 200
  • 2
  • 15
  • I looked up some sources and also found that this issue has been around for several years, there seem to be two situations where the protection can be removed (https://learn.microsoft.com/en-us/azure/information-protection/configure-policy-migrate-labels#footnote-1), you can submit your question to [Office VBA support and feedback](https://learn.microsoft.com/en-us/office/vba/articles/feedback-support). – Junjie Zhu - MSFT Aug 01 '22 at 08:20
  • Are you committed to the idea of manipulating Outlook? Emails can be sent via the smtplib library, provided your email service permits smtp access. If this is of interest I can link to a previous SO answer about it – jonathanfuchs Aug 08 '22 at 15:04
  • @jonathanfuchs could you please link the previous answer you were referring to? – DaniB Oct 27 '22 at 11:42
  • One example is this: https://stackoverflow.com/questions/64505/sending-mail-from-python-using-smtp/17596848#17596848 That answer includes the essential elements, other than the ehlo() and starttls() calls, which were particular to gmail in 2013. Whichever email service you pick will require other similar calls particular to whichever service, which can be best found via searching "smtplib" and whichever email provider – jonathanfuchs Oct 28 '22 at 22:37

1 Answers1

0

You could try this code instead.

import win32com.client as win32

olApp =win32.Dispatch("Outlook.Application")
olNS=olApp.GetNameSpace("MAPI")
mailItem= olApp.CreateItem(0)
mailItem.Subject="Hello my friend2"
mailItem.BodyFormat=1
mailItem.Body="This is the body of the email"
#mailItem.BodyFormat=2
#mailItem.HTMLBody="<HTLM Markup>"
mailItem.To="emailaddress@email.com"
#The numbers in the Invoke function do not change that
mailItem._oleobj_.Invoke(*(64209,0,8,0,olNS.Accounts.Item("emailaddress@email.com")))
mailItem.Display()
mailItem.Save()
mailItem.Send()

Be careful with the OlItemType enumeration the OlItemType enumeration and OlBodyFormat Enum.

Have a good one!

  • Hi Santiago, thanks for your reply. When I run the above code I get the following error "com_error: (-2147352571, 'Type mismatch.', None, 1)". By any chance do you know what this might be due to? – DaniB Aug 05 '22 at 13:07
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 06 '22 at 05:04