简体   繁体   English

Python exchangelib:检查项目是否为消息

[英]Python exchangelib: check if item is a message or not

I got an error when retrieve items using exchangelib. 使用exchangelib检索项目时出现错误。 Is there any method to detect whether the item is an email, and if not, just ignore it? 是否有任何方法可以检测该项目是否为电子邮件,如果不是,则忽略它? The following code raises AttributeError: 'MeetingRequest' object has no attribute 'flag') since meeting requests don't have a flag field. 下面的代码引发AttributeError: 'MeetingRequest' object has no attribute 'flag')因为会议请求没有flag字段,所以AttributeError: 'MeetingRequest' object has no attribute 'flag') Or is there any method to view the type of the item? 还是有什么方法可以查看商品的类型?

import re
import sys
from exchangelib import DELEGATE, IMPERSONATION, Account, Credentials, ServiceAccount, \
    EWSDateTime, EWSTimeZone, Configuration, NTLM, CalendarItem, Message, \
    Mailbox, Attendee, Q, ExtendedProperty, FileAttachment, ItemAttachment, \
    HTMLBody, Build, Version

from datetime import datetime, timedelta
import pytz

tz = EWSTimeZone.timezone('Asia/Hong_Kong')

creds = Credentials(
    username='domain\userID', 
    password='password')
)
account = Account(
    primary_smtp_address='myemail@domain',
    credentials=creds, 
    autodiscover=True, 
    access_type=DELEGATE)

class Flag(ExtendedProperty):
    property_tag = 0x1090
    property_type = 'Integer'

Message.register('flag', Flag)  


if(len(sys.argv) == 1):
    yesterday = tz.localize(EWSDateTime.now() - timedelta(days=3))
    today = tz.localize(EWSDateTime.now())
    fYear= yesterday.year
    fMonth= yesterday.month
    fDay= yesterday.day
    tYear = today.year
    tMonth = today.month
    tDay = today.day
elif(len(sys.argv) == 3):
    fromDate = sys.argv[1]
    toDate = sys.argv[2]
    fYear = fromDate[:4]
    fMonth = fromDate[4:6]
    fDay = fromDate[-2:]
    tYear = toDate[:4]
    tMonth = toDate[4:6]
    tDay = toDate[-2:]

for item in account.inbox.filter(datetime_received__range=(
    #tz.localize(EWSDateTime.now() - timedelta(days=1)),
    #tz.localize(EWSDateTime.now())
    tz.localize(EWSDateTime(int(fYear), int(fMonth), int(fDay))),
    tz.localize(EWSDateTime(int(tYear), int(tMonth), int(tDay)))
    )):
    subA=item.subject
    snd=item.sender.email_address
    fg=str(item.flag)
str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%a %b %d %H:%M:%S %Y"))
        rT = str(item.datetime_received.astimezone(pytz.timezone('Asia/Hong_Kong')).strftime("%Y-%m-%d %H:%M:%S"))
        cat=str(item.categories)
        if not subA:
            subA=""
            a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
            print(a)
        else:
            subA = re.sub('\"\,\"', '\\"\,\\"', subA.rstrip())
            a="\"Inbox\",\""+snd+"\",\""+subA+"\",\""+rT+"\",\""+cat+"\",\""+fg+"\""
            decoded = a.encode('utf-8').decode('utf-8')
            print(decoded)

Just check the class of the returned item: if type(item) == Message and ignore the ones that don't match. 只需检查返回项的类: if type(item) == Message并忽略不匹配的项。

If you want to avoid the cause of the error, you need to also register the flag property on the MeetingRequest class (and MeetingResponse and MeetingCancellation if you have those in your folder). 如果要避免导致错误的原因,还需要在MeetingRequest类上注册flag属性(如果文件夹中有那些属性,则还要注册MeetingResponseMeetingCancellation )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM