![](/img/trans.png)
[英]How do I monitor a different folder (than the default) within Outlook for new mail (and then fire an event) with Python
[英]How to continuously monitor a new mail in outlook and unread mails of a specific folder in python
我想檢查特定的發件人電子郵件,並在到達目的地時自動進行處理
但是,可能在某些情況下重新啟動了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,不如嘗試為該電子郵件設置Outlook規則,然后通過vba啟動python腳本。
這是從VBA啟動python腳本的代碼。
注意:以下代碼是從此處獲取的 。
Sub UruchomBata(MyMail As MailItem) Dim Ret_Val Dim args As String args = "c:\\new.py" Ret_Val = Shell("C:\\python27\\python.exe" & " " & args, vbNormalFocus) End Sub
這行下面是有興趣的Python腳本。 當前設置為控制com1串行端口上的DTR引腳。 需要將以下內容另存為yourname.py文件
import serial from time import sleep conn = serial.Serial('com1', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1, xonxoff=0, rtscts=0 ) # Wake Modem conn.setDTR(True) sleep(3) conn.setDTR(False) sleep(1) conn.close() # Start talking try: while True: conn.write('AT'+chr(13)); print conn.readline() # readlines() will probably never return. finally: conn.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.