'Unable to send mail through outlook using Python
I am using the below code to send email with an attachment using python. I using the outlook application itselt(not via backend)
from time import sleep
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = "EMAIL ADDRESS"
mail.Subject = "Subject"
mail.HtmlBody = "HTML Body"
mail.Attachments.Add("folderName\\output.zip")
mail.Display(True)
sleep(1)
mail.Send()
Its working fine till the line mail.Display(True). I can see the outlook new mail window open with everything typed in and also file attached. But the next statement mail.send() is giving error :
Traceback (most recent call last): File "C:/Users/username/PycharmProjects/001.PySelenium/win32email.py", line 16, in <module>
mail.send() File "C:\Users\username\PycharmProjects\001.PySelenium\venv\lib\site-packages\win32com\client\dynamic.py", line 516, in __getattr__
ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1) pywintypes.com_error: (-2147467260, 'Operation aborted', None, None)
Debug Screenshot :
Solution 1:[1]
Please use mail.Send()
By the way, if you want send mail through outlook using Python automatically.
Please comment mail.Display(True) and sleep(1), or you cannot send a mail until you manually save changes to the mail.
Please try:
import win32com.client
outlook = win32com.client.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = "[email protected]"
mail.Subject = "Test"
mail.Body = "Mail"
mail.Attachments.Add("folderName\\output.zip")
mail.Send()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

