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/

  • 相关阅读:
    7.2对象的生命周期
    7.2.2垃圾收集和对象的终结
    7.1.2验证
    发现电脑上装着liteide,就用golang做一个TCP通讯测试(支持先启动client端和断线重连)
    C++读写局域网共享
    C++编写 动态链接库dll 和 调用dll
    VBA果然很强大
    [Windows]查看运行进程的参数【wmic】
    自旋锁-SpinLock(.NET 4.0+)
    C#并行和多线程编程
  • 原文地址:https://www.cnblogs.com/skyer/p/6728112.html
Copyright © 2011-2022 走看看