Create and display an email

Adam Endicott leftwing17 at gmail.com
Thu Sep 15 16:37:04 EDT 2005


Thanks for the MAPI suggestion. I did a bit more googling, and combined
a few things, along with some trial and error to come up with something
that works. Here it is for posterity:

================================
import win32com.client
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application.11")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = 'A subject line'
newMail.Body = 'Body, wonderful body'
newMail.To = 'somebody at example.com'
filename = r'c:\test.txt'
newMail.Attachments.Add(filename)
newMail.Display()
================================

Everything I saw just had "Outlook.Application" on line 3, but that was
giving me an error:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

Looking in the makepy generated file for Outlook, I saw:

# This CoClass is known by the name 'Outlook.Application.11'

referring to the Application class. So I tried that and it worked. I
assume that the number will need to be changed for different versions
of Outlook, or perhaps is only necessary for 11 (11+?).

The other thing to note is that this still gave me an error:

pywintypes.com_error: (-2147024770, 'The specified module could not be
found.', None, None)

if Outlook was not actually open and running. Also, I believe the
filename for the attachment needs to be an absolute path. But with
Outlook open, this code works and displays an email message with the
specified Subject, Body, addressee, and attachment.

Hopefully somebody else can find this useful.




More information about the Python-list mailing list