code as following:
1import win32com.client, sqlite3 2from datetime import datetime 3 4def collectMail(): 5 conn = sqlite3.connect('outlook.db') 6 i = 0 7 try: 8 outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 9 10 inbox = outlook.GetDefaultFolder(6) 11 messages = inbox.Items 12 print 'total messages: ', len(messages) 13 message = messages.GetFirst () 14 while message: 15 i += 1 16 try: 17 subject = message.Subject 18 #print i, subject 19 20 received_time = str(message.ReceivedTime) 21 #print received_time 22 received_time = datetime.strptime(received_time, '%m/%d/%y %H:%M:%S') 23 #print message.EntryID 24 html_body = message.HTMLBody 25 size = long(message.Size) 26 27 sender = message.SenderName 28 receiver = message.To 29 cc = message.Cc 30 body = message.Body 31 conn.execute('insert into outlook(SUBJECT, SENDER, RECEIVER, CC, SIZE, RECEIVED_TIME, BODY, HTML_BODY) values(?, ?, ?, ?, ?, ?, ?, ?)', (subject, sender, receiver, cc, size, received_time, body, html_body)) 32 conn.commit() 33 except: 34 print i, 'skip' 35 continue 36 37 message = messages.GetNext() 38 finally: 39 print 'connection closed' 40 conn.close() 41 42 43collectMail() 44 45''' 46sql to create table 47 48create table outlook( 49ID INTEGER PRIMARY KEY AUTOINCREMENT, 50SUBJECT VARCHAR(200) NOT NULL, 51SENDER VARCHAR(200) NOT NULL, 52RECEIVER VARCHAR(200) NOT NULL, 53CC VARCHAR(200) NOT NULL, 54SIZE LONG NOT NULL, 55RECEIVED_TIME DATETIME, 56BODY TEXT, 57HTML_BODY TEXT); 58'''
python-script-to-read-the-emails-from-custom-folder-from-microsoft-outlook-mailb
reading-e-mails-from-outlook-with-python-through-mapi
microsoft.office.interop.outlook
sqlite_using_autoincrement