Python Forum
Need Outlook send email code using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need Outlook send email code using python
#1
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
Reply
#2
Please share your code snippet and detail us the difficulty you are facing.
Reply
#3
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)
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search Outlook Inbox for set of values cubangt 1 1,058 Jun-28-2023, 09:29 PM
Last Post: cubangt
  Save image from outlook email cubangt 1 694 Jun-07-2023, 06:52 PM
Last Post: cubangt
  Send email with smtp without using Mimetext mgallotti 0 706 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,311 Nov-14-2022, 09:31 PM
Last Post: malonn
  code to send attachments contained on the drive. stefanoste78 1 849 Oct-12-2022, 02:16 AM
Last Post: Larz60+
  Unable to send email attachments cosmarchy 7 2,534 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  Mark outlook emails as read using Python! shane88 2 6,540 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Trying out the parsing/reading of emails from my outlook cubangt 0 6,150 Jan-12-2022, 08:59 PM
Last Post: cubangt
  Help Needed | Read Outlook email Recursively & download attachment Vinci141 1 4,075 Jan-07-2022, 07:38 PM
Last Post: cubangt
  How to make scraper send email notification just once themech25 0 1,380 Nov-08-2021, 01:51 PM
Last Post: themech25

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020