The one thing that I see in this code is:
   * there is a possible problem with how you are calling the 
Outlook.Application object, namely you are doing a "GetActiveObject" and this 
could fail if Outlook is not already open.

If you are sure that Outlook will always be open when you run your python code 
(or this was an easy way to show an example), then it shouldn't be a problem, 
but I just wanted to make sure that wasn't throwing a wrench into the mix that 
you weren't thinking about.

I'm using pywin32 v304 on Python 3.10 (x64) on Windows 10 x64 (21H2) with 
Outlook 2019 (not the Ofifce 365 version), and your code works fine for me so 
it's not a W10 thing or a v304 on W10 issue).
 
I went as far as this code (slightly different at the end but basically the 
same):

import win32com.client

outlook = 
win32com.client.GetActiveObject('Outlook.Application').GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6).Items
messages = list(inbox)
for message in messages:
    print(message.Subject)

...and all was well.

#snipped output start
Backup Log: XXXXXXXXXX
WSUS: Update Status Summary From XXXXXXXXXX
WSUS: Update Status Summary From XXXXXXX
#snipped output end

Here's another possible problem: 
    * the attached security warning popped up a few times while I was testing 
your code, and this could also be giving your python code issues (if the 
approve button isn’t pressed in X seconds or the deny button is pressed). 

 Back in the day of Outlook 2003, there were ways to publish something to your 
Public Folders in a specific folder path to tell Outlook that it should allow 
programmatic access (no matter what) for user X or Group Y, but just remember, 
most of the Outlook Automation that people typically have seen is created by 
hackers, so Microsoft had to lock it down a lot, and as such,  this type of 
warning shouldn't really be disabled except for maybe 1 - 5 minutes at a time 
by someone running the script who understands exactly what it's doing (and even 
then you are opening yourself to a security nightmare if that system gets a 
virus that tries to do some outlook automation).

If you wanted to write a Macro, and sign it with a Code Signing Certificate 
from a trusted Certificate Authority we MIGHT be able to make something work, 
but the Outlook automation security is there for a reason and shouldn't be 
messed with (PLUS: I've never written a Python macro for Outlook).  

Note: you can change the Outlook security settings to run Signed Macros 
relatively easy without any security warnings popping up (provided your system 
trusts the CA supplying the Code Signing Certificate and the certificate is not 
expired, etc).  I currently have some VBScript code that will move Mail 
messages to an Exchange public folder and write some data to my SpamAssassin 
server telling SpamAssassin that a mail is "Good" or "Bad" every time a message 
(or multiple messages) are selected and a macro button is pressed in the 
Outlook client (it stops working when I let my certificate expire  ☹ ).

I hope these points help you diagnose this problem further, and or give you 
more ideas on how to automate your task within the Outlook automation security 
framework.

Steven
-----Original Message-----
From: python-win32 <python-win32-bounces+steven=manross....@python.org> On 
Behalf Of m...@wampenseppl.de
Sent: Sunday, May 15, 2022 3:08 AM
To: python-win32@python.org
Subject: [python-win32] Reading email body from Outlook: Operation aborted

Dear all,

(Transparency note: This has been posted to 
https://python-forum.io/thread-37103.html before)

The following code will print the ReceivedTime as well as the email body on a 
Win11 machine (pywin32 version 304, personal computer):

   import win32com.client

   outlook = 
win32com.client.GetActiveObject('Outlook.Application').GetNamespace("MAPI")
   inbox = outlook.GetDefaultFolder(6).Items
   messages = list(inbox)

   print(messages[0].ReceivedTime)
   print(messages[0].Body)

On a second machine (Win10, same version of pywin32, company device), execution 
will fail:

   PS C:\Users\xxx> & 
C:/Users/xxx/AppData/Local/Programs/Python/Python310/python.exe 
"c:/Users/xxx/outlook.py"
   2021-05-17 16:47:59.781000+00:00
   Traceback (most recent call last):
     File "c:\Users\xxx\outlook.py", line 8, in <module>
       print(messages[0].Body)
     File 
"C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py",
 line 628, in __getattr__    
       ret = self._oleobj_.Invoke(retEntry.dispid, 0, invoke_type, 1)
   pywintypes.com_error: (-2147467260, 'Operation aborted', None, None)

It can read the ReceivedTime, but not the Body. I think it has to do with 
Outlook security policy as set up on the company device. I've already applied 
the registry keys mentioned at 
https://www.slipstick.com/developer/change-programmatic-access-options/, but 
couldn't see any changed behavior. Also, I can't run Outlook (O365 / V2204) as 
an admin as this will request me to create a new account. I was trying to 
access Trust Center -> Programmatic Access there. Could anybody kindly guide me 
to the correct direction, please?

Best regards,
Timo
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to