gpt4 book ai didi

python - 如何在Outlook中连续监视新邮件和python中特定文件夹的未读邮件

转载 作者:行者123 更新时间:2023-11-28 21:37:20 25 4
gpt4 key购买 nike

我想检查特定的发件人电子邮件,并在到达目的地时自动进行处理

但是,可能在某些情况下重新启动了Outlook,这意味着我收到了发件人的邮件并标记为未读

为了连续监视特定主题的新邮件,我找到了以下代码

import win32com.client
import pythoncom
import re

class Handler_Class(object):
def OnNewMailEx(self, receivedItemsIDs):
# RecrivedItemIDs is a collection of mail IDs separated by a ",".
# You know, sometimes more than 1 mail is received at the same moment.
for ID in receivedItemsIDs.split(","):
mail = outlook.Session.GetItemFromID(ID)
subject = mail.Subject
print subject
try:
command = re.search(r"%(.*?)%", subject).group(1)

print command # Or whatever code you wish to execute.
except:
pass


outlook = win32com.client.DispatchWithEvents("Outlook.Application",Handler_Class)

#and then an infinit loop that waits from events.
pythoncom.PumpMessages()

甚至我也想浏览所有未读的邮件,以检查是否有来自发件人的邮件来处理它(如果找到)

是否有任何功能可以检查要添加到handler_class中的未读邮件

或让我知道其他替代程序

最佳答案

因此,如果您每次Outlook重新启动时都重新启动python脚本,则将以下几行添加到代码中,以检查收件箱中的未读电子邮件:

ol = win32com.client.Dispatch( "Outlook.Application")
inbox = ol.GetNamespace("MAPI").GetDefaultFolder(6)
for message in inbox.Items:
if message.UnRead == True:
print message.Subject #or whatever command you want to do

将此代码放在代码中对 outlook的定义之前

编辑

对我来说,您发布的代码在关闭Outlook之前非常有用,然后即使我重新打开它,也不会在收到新消息时收到任何信息(请参阅我的评论之一)。我猜想用 pythoncom.PumpMessages()关闭Outlook“unlink”的事实。无论如何,我都会来检查您在 Handler_Class类中的未读电子邮件,并在您重新启动Outlook的情况下重新启动监视。
import win32com.client
import ctypes # for the VM_QUIT to stop PumpMessage()
import pythoncom
import re
import time
import psutil

class Handler_Class(object):

def __init__(self):
# First action to do when using the class in the DispatchWithEvents
inbox = self.Application.GetNamespace("MAPI").GetDefaultFolder(6)
messages = inbox.Items
# Check for unread emails when starting the event
for message in messages:
if message.UnRead:
print message.Subject # Or whatever code you wish to execute.

def OnQuit(self):
# To stop PumpMessages() when Outlook Quit
# Note: Not sure it works when disconnecting!!
ctypes.windll.user32.PostQuitMessage(0)

def OnNewMailEx(self, receivedItemsIDs):
# RecrivedItemIDs is a collection of mail IDs separated by a ",".
# You know, sometimes more than 1 mail is received at the same moment.
for ID in receivedItemsIDs.split(","):
mail = self.Session.GetItemFromID(ID)
subject = mail.Subject
print subject
try:
command = re.search(r"%(.*?)%", subject).group(1)
print command # Or whatever code you wish to execute.
except:
pass

# Function to check if outlook is open
def check_outlook_open ():
list_process = []
for pid in psutil.pids():
p = psutil.Process(pid)
# Append to the list of process
list_process.append(p.name())
# If outlook open then return True
if 'OUTLOOK.EXE' in list_process:
return True
else:
return False

# Loop
while True:
try:
outlook_open = check_outlook_open()
except:
outlook_open = False
# If outlook opened then it will start the DispatchWithEvents
if outlook_open == True:
outlook = win32com.client.DispatchWithEvents("Outlook.Application", Handler_Class)
pythoncom.PumpMessages()
# To not check all the time (should increase 10 depending on your needs)
time.sleep(10)

不确定这是最好的方法,但是它似乎可以按照您的期望工作。

关于python - 如何在Outlook中连续监视新邮件和python中特定文件夹的未读邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49695160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com