Python Forum
Need Outlook send email code using python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need Outlook send email code using python (/thread-27989.html)



Need Outlook send email code using python - srikanthpython - Jun-30-2020

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


RE: Need Outlook send email code using python - Malt - Jun-30-2020

Please share your code snippet and detail us the difficulty you are facing.


RE: Need Outlook send email code using python - srikanthpython - Jul-01-2020

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)



RE: Need Outlook send email code using python - asyswow64 - Feb-28-2021

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)