Wednesday, 10 October 2012

Read Outlook mail with python


import win32com.client

session = win32com.client.gencache.EnsureDispatch ("MAPI.Session")

#
# Leave blank to be prompted for a session, or use
# your own profile name if not "Outlook". It is also
# possible to pull the default profile from the registry.
#
session.Logon ("Default Outlook Profile")
messages = session.Inbox.Messages

message = messages.GetFirst ()

# Here are all the attributes available for the message

print "Application - ",message.Application
print "Attachments - ",message.Attachments
print "Categories - ",message.Categories
print "Class - ",message.Class
print "ConversationIndex - ",message.ConversationIndex
print "ConversationTopic - ",message.ConversationTopic
print "DeliveryReceipt - ",message.DeliveryReceipt
print "Encrypted - ",message.Encrypted
print "Fields - ",message.Fields
print "FolderID - ",message.FolderID
print "ID - ",message.ID
print "Importance - ",message.Importance
print "MAPIOBJECT - ",message.MAPIOBJECT
print "Parent - ",message.Parent
print "ReadReceipt - ",message.ReadReceipt
print "Recipients - ",message.Recipients
print "Sender - ",message.Sender
print "Sensitivity - ",message.Sensitivity
print "Sent - ",message.Sent
print "Session - ",message.Session
print "Signed - ",message.Signed
print "Size - ",message.Size
print "StoreID - ",message.StoreID
print "Subject - ",message.Subject
print "Submitted - ",message.Submitted
print "Text - ",message.Text
print "TimeCreated - ",message.TimeCreated
print "TimeLastModified - ",message.TimeLastModified
print "TimeReceived - ",message.TimeReceived
print "TimeSent - ",message.TimeSent
print "Type - ",message.Type
print "Unread - ",message.Unread

9 comments:

  1. hi dude, all attributes are not working. I'm using outlook 2010.
    What can be the problem?

    ReplyDelete
    Replies
    1. Have tried this with outlook 2010, just let me know which attributes are not working.

      Delete
    2. #print "DeliveryReceipt - ",message.DeliveryReceipt
      #print "Encrypted - ",message.Encrypted
      #print "Fields - ",message.Fields
      #print "FolderID - ",message.FolderID
      #print "ID - ",message.ID
      #print "Text - ",message.Text
      #print "TimeCreated - ",message.TimeCreated
      #print "TimeLastModified - ",message.TimeLastModified
      #print "TimeReceived - ",message.TimeReceived
      #print "TimeSent - ",message.TimeSent
      #print "Type - ",message.Type

      Delete
  2. Great help bro.. Actually I was doing another type of operation but I used .GetLast() instead of .GetFirst() due to lack of identifying the mail receiving logic. But after analyzing your program, I got to know. Thank you so much. You can find me in GitHub >> https://github.com/deb991. Thank you so much for your help.

    ReplyDelete
  3. I am unable to install win32com.client. Can you please help ? I am using Python 3.3.5 on Windows 7 32bit OS

    ReplyDelete
  4. how to get sender mailid from ReadReceipt from inbox?

    ReplyDelete
    Replies
    1. I got this error....


      Traceback (most recent call last):
      File "D:\mathi\tutorial\tutorial.py", line 182, in
      print ("DeliveryReceipt - ",msg.Sender)
      File "C:\Program Files (x86)\Python35-32\lib\site-packages\win32com\client\__init__.py", line 473, in __getattr__
      raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
      AttributeError: '' object has no attribute 'Sender'
      [Finished in 0.4s with exit code 1]
      [shell_cmd: python -u "D:\mathi\tutorial\tutorial.py"]
      [dir: D:\mathi\tutorial]
      [path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files\ImageMagick-7.0.8-Q16;C:\Program Files (x86)\GnuWin32\bin;C:\Program Files (x86)\GnuWin32;C:\Program Files\ImageMagick-6.9.5-Q16-HDRI;C:\Program Files\ImageMagick-6.9.5-Q8;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Python35-32\Scripts;C:\Program Files (x86)\Python35-32;C:\Program Files\nodejs\;C:\Program Files\Sublime Text 3;C:\PostgreSQL\pg11\bin;C:\Psql\pg11\bin;C:\Program Files (x86)\Python35-32\tcl\tcl8.6;C:\Program Files\ImageMagick-7.0.8-Q16;C:\Go\bin;C:\Users\scraping.HAWKI09\AppData\Roaming\npm;C:\Program Files\Sublime Text 3;C:\Users\scraping.HAWKI09\go\bin]

      Delete