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/

  • 相关阅读:
    centos7 安装kafka Manager
    MySql Table错误:is marked as crashed and last (automatic?) 和 Error: Table "mysql"."innodb_table_stats" not found
    安装prometheus+grafana监控mysql redis kubernetes等
    centos7 安装kubernetes1.4
    linux ip 转发设置 ip_forward
    开启Tomcat远程调试(转)
    SSH自动断开连接的原因、配置(转)
    解决mysql启动时报The server quit without updating PID file 的错误(转)
    supervisor的集中化管理搭建
    supervisor安装配置
  • 原文地址:https://www.cnblogs.com/skyer/p/6728112.html
Copyright © 2011-2022 走看看