简体   繁体   English

如何使用python获取收到的Outlook邮件的日期和时间并检查来自特定发件人的正文

[英]How to get date & time of received outlook mail and check body from particular sender using python

I am trying to achieve the following using python: get the last 24hrs outlook inbox mails from the particular sender and checking if a particular text is presented in the body of that email我正在尝试使用 python 实现以下目标:从特定发件人获取最近 24 小时的 Outlook 收件箱邮件,并检查该电子邮件正文中是否显示了特定文本

below is I tried :下面是我试过的:

import sys, win32com.client, datetime
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
s = outlook.GetDefaultFolder(6).Items   
# Get yesterdays date for the purpose of getting emails from this date
d = (datetime.date.today() - datetime.timedelta (days=1)).strftime("%d-%m-%y")
# get the email/s
msg = s.GetLast()
# Loop through emails
while msg:
    # Get email date 
    date = msg.SentOn.strftime("%d-%m-%y")
    # Set the critera for whats wanted                       
    if (d == date) and ("MAINTENANCE" in msg.body) and ("Upgrade" in msg.Subject) and msg.Sender == "aa@bb.com" :
        print("Date : ", date) 
    msg = s.GetPrevious() 

error :错误 :

date = msg.SentOn.strftime("%d-%m-%y") File "C:\\Users\\1024983\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32com\\client\\dynamic.py", line 527, in getattr raise AttributeError("%s.%s" % (self. username , attr)) AttributeError: GetPrevious.SentOn date = msg.SentOn.strftime("%d-%m-%y") 文件 "C:\\Users\\1024983\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages\\win32com\\client\\dynamic. PY”,线路527,在GETATTR加注AttributeError的( “%S%S” %(自用户名,ATTR)。)AttributeError的:GetPrevious.SentOn

and notsure what is the error in the checking line并不确定检查行中的错误是什么

if (d == date) and ("MAINTENANCE" in msg.body) and ("Upgrade" in msg.Subject) and msg.Sender == "aa@bb.com" :

Firstly, never loop through all item in a folder - you wouldn't use a SELECT statement in SQL without a WHERE clause, would you?首先,永远不要遍历文件夹中的所有项目——如果没有 WHERE 子句,你不会在 SQL 中使用 SELECT 语句,对吗? Always use Items.Find/FindNext or Items.Restrict .始终使用Items.Find/FindNextItems.Restrict

Secondly, Sender is not a scalar (string) property - it returns an object ( AddressEntry ), so what you want is MailItem.Sender.Address (after checking that MailItem.Sender is no null).其次, Sender不是标量(字符串)属性 - 它返回一个对象( AddressEntry ),所以你想要的是MailItem.Sender.Address (在检查MailItem.Sender不为空之后)。

Thirdly, besides MailItem objects in your Inbox folder, you can also have other items, such as MeetingItem and ReportItem whcih may not expose all the properties you are accessing (such as Sender ) .第三,除了收件箱文件夹中的MailItem对象之外,您还可以拥有其他项目,例如MeetingItemReportItemReportItem可能不会公开您正在访问的所有属性(例如Sender )。 If you only want MailItem objects, check that the Class property (exposed by all OOM objects) == 43 ( OlObjectClass.olMail ).如果您只需要MailItem对象,请检查Class属性(由所有 OOM 对象公开)== 43 ( OlObjectClass.olMail )。

暂无
暂无

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

相关问题 如何使用python脚本访问outlook中特定发件人的最新邮件 - How to access latest mail from a specific sender in outlook using python script 如何使用 Python 从 Outlook 中突出显示(选定)邮件? - How to get highlighted (selected) mail from outlook using Python? 如何使用 Python 保存来自特定发件人和日期的 MS Outlook 附件 - How to save MS Outlook attachments from specific sender and date using Python 如何使用 Z23EEEB4347BDD255DFC6B7EE9A3 从 outlook 中的 MailItem 获取发件人 SMTP email 地址? - How to get sender SMTP email address from MailItem in outlook using python? 如何使用Python脚本在Outlook中提取收到的电子邮件的日期? - How to extract the date of the received emails in Outlook using Python scripting? 检查昨天收到的Outlook文件夹中是否有任何邮件 - Check if there is any mail in an outlook folder received yesterday 基于 Python 的 Outlook 自动化使用来自 Excel 文件的数据作为邮件正文并添加签名 - Python-based automation of Outlook using data from an Excel file for the mail body and adding a signature 如何使用 Python 从 Outlook 帐户发送带有附件的邮件 - how to Send mail with attachment from your outlook account using Python 无法按收到日期的顺序从 outlook 中的某个文件夹中获取邮件 - Not able to get mails from a certain folder in outlook in the order of their received date Python:有人知道msg Outlook文件的“已接收邮件日期”参数吗? - Python: Does any one knows the Received mail date Parameter for msg Outlook file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM