首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Pywin32访问Outlook中的电子邮件发件人

如何使用Pywin32访问Outlook中的电子邮件发件人
EN

Stack Overflow用户
提问于 2021-06-14 13:32:02
回答 1查看 304关注 0票数 0

例如,我想打印一些关于Category=“积压”的电子邮件的信息。我使用pywin32库的代码如下:

代码语言:javascript
复制
import win32com.client
import os, sys
from datetime import date, datetime, timedelta

def print_messages(messages, sCategory, f):
    msgs = messages.Restrict("[Categories] = '" + sCategory + "'")
    for message in list(msgs):
        try:
            if message.Class == 43:
                if message.SenderEmailType == "EX":
                    strSender = message.Sender.GetExchangeUser().PrimarySmtpAddress
                else:
                    strSender = message.SenderEmailAddress
            f.write(message.EntryID + "\t" + strSender + message.ConversationTopic + "\t" + message.ReceivedTime.strftime("%Y-%m-%d %H:%M") + "\t" + message.Categories + "\n")
        except Exception as e:
            print("Oops!", sys.exc_info()[0], "occurred.")

def main():
    outlook = win32com.client.Dispatch('outlook.application').GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6)
    messages = inbox.Items
    now = datetime.now()
    f = open("C:/Temp/outlook_emails_" + now.strftime("%Y-%m-%d_%H-%M-%S") + ".txt", "a")
    print_messages(messages, "Backlog", f)
    f.close()

if __name__ == "__main__":
    main()

当此代码运行时,它无法接收电子邮件发件人。异常会发生,它打印的行如下:

代码语言:javascript
复制
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.
Oops! <class 'pywintypes.com_error'> occurred.

在行中的问题:strSender = message.Sender.GetExchangeUser().PrimarySmtpAddress和行strSender = message.SenderEmailAddress。不知道如何正确访问发件人的电子邮件地址!!

使用的版本:

  • Windows 10 (64位)
  • Outlook v16 (64位)
  • Python3.9.5(64位)
  • pywin32版本: 300
EN

Stack Overflow用户

发布于 2021-06-14 17:25:08

您从不检查GetExchangeUser是否返回有效的对象,而不是null。当前配置文件必须包含容纳GAL用户的原始Exchange服务器。

在使用GetExchangeUser之前,请检查PidTagSenderSmtpAddress MAPI属性是否可用message.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x5D01001F")

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67971195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档