简体   繁体   English

如何使用 Python 从 Outlook 中突出显示(选定)邮件?

[英]How to get highlighted (selected) mail from outlook using Python?

I can not understand how to parse highlighted (selected) mail from outlook using Python?我不明白如何使用 Python 解析 Outlook 中突出显示(选定)的邮件?

I have this code, but it works with last mail.我有这个代码,但它适用于最后一封邮件。

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items
message = messages.GetLast()
body_content = message.body
print (body_content)

Need to parse sender email address of highlighted mail?需要解析突出显示邮件的发件人电子邮件地址吗?

Should be应该

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application")
messages = outlook.ActiveExplorer().Selection
message = messages(1)

try:
    if message.SenderEmailType == "EX":
        print("EX: ", message.Sender.GetExchangeUser().PrimarySmtpAddress)
    else:
        if message.SenderEmailType == "SMTP":
            print("SMTP: ", message.SenderEmailAddress)
except Exception as e:
    print(e)

Use Application.ActiveExplorer.Selection.Item(1) to retrieve the currently selected message.使用Application.ActiveExplorer.Selection.Item(1)检索当前选择的消息。 Your code retrieves the last email in the Inbox - whatever "last" is, since you never explicitly sort the items collection.您的代码检索收件箱中的最后一封电子邮件 - 无论“最后一封”是什么,因为您从未明确对项目集合进行排序。 Most likely you will end up with the oldest email in the Inbox.您很可能会收到收件箱中最旧的电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python获取收到的Outlook邮件的日期和时间并检查来自特定发件人的正文 - How to get date & time of received outlook mail and check body from particular sender using python 如何使用 Python 从 Outlook 帐户发送带有附件的邮件 - how to Send mail with attachment from your outlook account using Python 使用 python 读取选定 Outlook 电子邮件的内容 - Read contents of a selected Outlook e-mail using python 如何使用python脚本访问outlook中特定发件人的最新邮件 - How to access latest mail from a specific sender in outlook using python script 如何从电子邮件下载附件并保留原始文件名? 使用Python / outlook - how to download attachments from e-mail and keep original filename? using Python/outlook 如何在Outlook中使用python将电子邮件与收件人作为Microsoft邮件发送电子邮件? - How to send email using python in outlook with recipient as microsoft mail? 如何使用python在Outlook(win app)中回复邮件? - How to reply to a mail in Outlook(win app) using python? 如何使用 python 在 docx 文件中获取突出显示的文本 - How to get highlighted text in a docx file using python 使用 Python 从 Outlook 获取 Email 列表 - Get Email list from Outlook using Python 想要使用 python 从最旧的邮件中获取 outlook 邮件到最新的邮件 - Want to fetch outlook mails from the oldest mail to the newest mail using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM