Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8624849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:38:02+00:00 2026-06-12T07:38:02+00:00

I’m attempting to start some Outlook 2007 automation with Python. I got this great

  • 0

I’m attempting to start some Outlook 2007 automation with Python. I got this great script (below) from Steve Townsend on this thread: Send Outlook Email Via Python?

but I’m having trouble getting started with this.

import win32com.client

def send_mail_via_com(text, subject, recipient, profilename="Outlook2007"):
    s = win32com.client.Dispatch("Mapi.Session")
    o = win32com.client.Dispatch("Outlook.Application")
    s.Logon(profilename)

    Msg = o.CreateItem(0)
    Msg.To = recipient

    Msg.CC = "moreaddresses here"
    Msg.BCC = "address"

    Msg.Subject = subject
    Msg.Body = text

    #attachment1 = "Path to attachment no. 1"
    #attachment2 = "Path to attachment no. 2"
    #Msg.Attachments.Add(attachment1)
    #Msg.Attachments.Add(attachment2)

    Msg.Send()


send_mail_via_com("test text","test subject", "removed@security.obv","Outlook2007")

But I get the following Errors:

Traceback (most recent call last):
  File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 83, in _
GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\PROJECTS\Python\send_mail_test.py", line 25, in <module>
    send_mail_via_com("test text","test subject", "removed@security.obv","Outloo
k2007")
  File "C:\PROJECTS\Python\send_mail_test.py", line 4, in send_mail_via_com
    s = win32com.client.Dispatch("Mapi.Session")
  File "C:\Python32\lib\site-packages\win32com\client\__init__.py", line 95, in
Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
  File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 108, in
_GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
  File "C:\Python32\lib\site-packages\win32com\client\dynamic.py", line 85, in _
GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

It’s probably something silly that I’ve missed.

It’s Python 3.2 and PyWin32 has been installed

Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:38:03+00:00Added an answer on June 12, 2026 at 7:38 am

    never mind…

    import win32com.client
    olMailItem = 0x0
    obj = win32com.client.Dispatch("Outlook.Application")
    newMail = obj.CreateItem(olMailItem)
    newMail.Subject = "I AM SUBJECT!!"
    newMail.Body = "I AM IN THE BODY\nSO AM I!!!"
    newMail.To = "who_to_send_to@example.com"
    #newMail.CC = "moreaddresses here"
    #newMail.BCC = "address"
    #attachment1 = "Path to attachment no. 1"
    #attachment2 = "Path to attachment no. 2"
    #newMail.Attachments.Add(attachment1)
    #newMail.Attachments.Add(attachment2)
    #newMail.display()
    newMail.Send()
    

    This works on Python 3.2.3 with PyWin32 installed. I have commented out some lines for if you want to play with this.

    [2017 EDIT] – adding HTML email support (in case it’s handy for anyone)

    import win32com.client
    
    #some constants (from http://msdn.microsoft.com/en-us/library/office/aa219371%28v=office.11%29.aspx)
    olFormatHTML = 2
    olFormatPlain = 1
    olFormatRichText = 3
    olFormatUnspecified = 0
    olMailItem = 0x0
    
    
    obj = win32com.client.Dispatch("Outlook.Application")
    newMail = obj.CreateItem(olMailItem)
    newMail.Subject = "I AM SUBJECT!!"
    newMail.BodyFormat = olFormatHTML    #or olFormatRichText or olFormatPlain
    newMail.HTMLBody = "<h1>I am a title</h1><p>I am a paragraph</p>"
    newMail.To = "who_to_send_to@example.com; anotherrecipient@email.fake"
    
    # carbon copies and attachments (optional)
    
    #newMail.CC = "moreaddresses here"
    #newMail.BCC = "address"
    #attachment1 = "Path to attachment no. 1"
    #attachment2 = "Path to attachment no. 2"
    #newMail.Attachments.Add(attachment1)
    #newMail.Attachments.Add(attachment2)
    
    # open up in a new window and allow review before send
    newMail.display()
    
    # or just use this instead of .display() if you want to send immediately
    #newMail.Send()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.