1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import win32com.client, sqlite3 from datetime import datetime
def collectMail(): conn = sqlite3.connect('outlook.db') i = 0 try: outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) messages = inbox.Items print 'total messages: ', len(messages) message = messages.GetFirst () while message: i += 1 try: subject = message.Subject #print i, subject
received_time = str(message.ReceivedTime) #print received_time received_time = datetime.strptime(received_time, '%m/%d/%y %H:%M:%S') #print message.EntryID html_body = message.HTMLBody size = long(message.Size)
sender = message.SenderName receiver = message.To cc = message.Cc body = message.Body 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)) conn.commit() except: print i, 'skip' continue
message = messages.GetNext() finally: print 'connection closed' conn.close()
collectMail()
|