Python Forum

Full Version: Need Outlook send email code using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

Is there any working code for sending email in python using Outlook 365?
I tried different codes which I found in google but nothing is working as expected. Please share code.

Thank you in advance.

Regards
Srikanth
Please share your code snippet and detail us the difficulty you are facing.
Hi Malt,

Here is the code

import win32com.client as win32

Outlook = win32.Dispatch('Outlook.application')
mail = Outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'python test'
mail.Body = 'test'
mail.HTMLBody = '<h2>HTML Message body</h2>'
mail.Send()
When I run tis script I am getting below error


Error:
Traceback (most recent call last): File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch IDispatch = pythoncom.connect(IDispatch) pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/Sri/PycharmProjects/PythonCoding/venv/Test.py", line 3, in <module> Outlook = win32.Dispatch('Outlook.application') File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
You need to make sure that outlook is closed before you run the script.

(Jul-01-2020, 03:15 AM)srikanthpython Wrote: [ -> ]Hi Malt,

Here is the code

import win32com.client as win32

Outlook = win32.Dispatch('Outlook.application')
mail = Outlook.CreateItem(0)
mail.To = '[email protected]'
mail.Subject = 'python test'
mail.Body = 'test'
mail.HTMLBody = '<h2>HTML Message body</h2>'
mail.Send()
When I run tis script I am getting below error


Error:
Traceback (most recent call last): File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 89, in _GetGoodDispatch IDispatch = pythoncom.connect(IDispatch) pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/Sri/PycharmProjects/PythonCoding/venv/Test.py", line 3, in <module> Outlook = win32.Dispatch('Outlook.application') File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx) File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 114, in _GetGoodDispatchAndUserName return (_GetGoodDispatch(IDispatch, clsctx), userName) File "C:\Users\Sri\PycharmProjects\PythonCoding\venv\lib\site-packages\win32com\client\dynamic.py", line 91, in _GetGoodDispatch IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch) pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)