zoukankan      html  css  js  c++  java
  • 使用win32com接口获取outlook收件箱的内容

    环境准备:

    1.安装pywin32安装包,https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/

    2.本地使用的outlook 2013

    #encoding=utf-8

    import win32com.client, sqlite3
    from datetime import datetime

    def collectMail():
    conn = sqlite3.connect(r'D:SoftwareSQLiteSpy_1.9.9outlook.db')
    i = 0
    try:

    #启动outlook进程
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    #获取收件箱实例
    inbox = outlook.GetDefaultFolder(6)

    #逐条处理邮件信息
    messages = inbox.Items
    print 'total messages: ', len(messages)
    message = messages.GetFirst()
    #print dir(message)
    i=0
    while message:
    try:
    #邮件标题
    if hasattr(message,"Subject"):
    subject = message.Subject
    #邮件接收时间
    if hasattr(message,"ReceivedTime"):
    received_time = str(message.ReceivedTime)
    received_time = datetime.strptime(received_time, "%m/%d/%y %H:%M:%S")
    if hasattr(message,"HTMLBody"):
    html_body = message.HTMLBody
    size = long(message.Size)
    #邮件发送人
    if hasattr(message,"SenderName"):
    sender = message.SenderName
    #邮件接收人
    if hasattr(message,"To"):
    receiver = message.To
    #邮件抄送人
    if hasattr(message,"Cc"):
    cc = message.Cc
    #邮件正文
    if hasattr(message,"Body"):
    body = message.Body
    #将邮件内容逐条插入outlook表中,? 为占位符
    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()
    #获取下条邮件内容
    message = messages.GetNext()
    i+=1
    print i,subject
    except Exception as e:
    print "error1:",e
    break
    except Exception as e:
    print "error2:",e
    finally:
    print 'connection closed'
    conn.close()

    collectMail()

    '''

    创建表的语句:
    sql to create table

    create table outlook(
    ID INTEGER PRIMARY KEY AUTOINCREMENT,
    SUBJECT VARCHAR(200) NOT NULL,
    SENDER VARCHAR(200) NOT NULL,
    RECEIVER VARCHAR(200) NOT NULL,
    CC VARCHAR(200) NOT NULL,
    SIZE LONG NOT NULL,
    RECEIVED_TIME DATETIME,
    BODY TEXT,
    HTML_BODY TEXT);
    '''

    参考地址:

    https://msdn.microsoft.com/en-us/library/office/aa155717(v=office.10).aspx

    https://www.laurivan.com/python-and-outlook-an-example/

  • 相关阅读:
    WordCount结对项目
    第一周作业:一些感想
    第一次作业
    Spring Cloud 微服务实战笔记
    解决jest处理es模块
    领域驱动设计(DDD:Domain-Driven Design)
    测试
    whistle
    日记(2018-11-07)
    ubuntu中使用机密数据Secrets
  • 原文地址:https://www.cnblogs.com/skyer/p/6728112.html
Copyright © 2011-2022 走看看