user3251889
user3251889

Reputation: 53

Send outlook email using python

I want to send outlook email via python and I found below script

import win32com.client
from win32com.client import Dispatch, constants
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "I AM SUBJECT!!"
newMail.BodyFormat = 2 
newMail.HTMLBody = "<HTML><BODY>Enter the <span style='color:red'>message</span> text here.</BODY></HTML>"
newMail.To = "[email protected]"
newMail.display()
newMail.Send()

Everything works fine untill newMail.Send(), it gives this error

Traceback (most recent call last):
  File "<ipython-input-46-7e8e370e48a8>", line 1, in <module>
    newMail.Send()
File "<COMObject CreateItem>", line 2, in Send
com_error: (-2147467260, 'Operation aborted', None, None)

Upvotes: 1

Views: 1815

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

You need to either display the message, or send it, but not both - your code displays the message, and then immediately sends it.

Upvotes: 1

Related Questions