2
votes

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 :

enter image description here

1
Remove true from display, Why display? you can remove all and just use mail.send()0m3r
Tried that based on the answer given by 'Strive', but didn't work.anandhu
Is pywin32 updated?0m3r
I have latest version 227anandhu
I have added screenshot on questionanandhu

1 Answers

0
votes

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()