February 11, 2009

Python - Send Email From Windows Using CDO

Here is quick script showing how to use Python to send email from Windows.

This approach uses the Python For Windows Extensions to access Outlook/Exchange with CDO (Collaboration Data Objects).

#!/usr/bin/env python
# Corey Goldberg

from win32com.client import Dispatch

session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nUserName');
msg = session.Outbox.Messages.Add('Hello', 'This is a test')
msg.Recipients.Add('Corey', 'SMTP:corey@foo.com')
msg.Send()
session.Logoff()

2 comments:

Matt said...

You should post the corresponding .Net code (which I wrote once). From what I recall it ain't anywhere near as succint.

Robert said...

that bloody Outlook security message is f**king everything up!