I am trying to send an email in a Windows 11 machine, with a Python code that was previously working on a different Windows 10 machine with Outlook installed.
It seems like, for some reason, in Windows 11, the "real" Outlook desktop app is not installed...instead, it installs some sort of (webview maybe??) version of Outlook that it calls "Outlook New".
Anyway, the issue is that, the original code below that was working before does no longer work.
import win32com.client as win32
outlook = win32.Dispatch("Outlook.Application")
mail = outlook.CreateItem(0)
mail.Subject = "Email Subject"
mail.Body = "Email body,Python win32com and Outlook."
mail.To = "[email protected]"
mail.Send()
This fails with traceback
Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\test_mail.py", line 4, in <module>
outlook = win32.Dispatch("Outlook.Application")
File "C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\__init__.py", line 118, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
File "C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\dynamic.py", line 104, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Users\admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\win32com\client\dynamic.py", line 87, in _GetGoodDispatch
IDispatch, None, clsctx, pythoncom.IID_IDispatch
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
Which suggests me that it seems to be a problem finding the "real" Outlook app.
Can anyone suggest a fix for this in Windows 11?
instead, it installs some sort of (webview maybe??) version of Outlook that it calls "Outlook New".
You are on the right avenue. The new Outlook is a progressive web app which doesn't expose any COM components. There is no way to automate such web applications in native shells. Instead, you may consider using the Graph API for your tasks. See Use the Microsoft Graph API for more information.