日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python如何读取outlook邮件_通过MAPI使用Python从Outlook中阅读电子邮件

發布時間:2023/12/20 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何读取outlook邮件_通过MAPI使用Python从Outlook中阅读电子邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

通過MAPI使用Python從Outlook中閱讀電子郵件

我試圖編寫一個簡短的程序,將讀取我的Exchange / Outlookconfiguration文件中的文件夾內的電子郵件內容,以便我可以操縱數據。 不過,我有一個問題,find有關Python和Exchange / Outlook集成的很多信息。 很多東西要么很舊/沒有文檔/沒有解釋。 我已經嘗試了幾個片段,但似乎得到相同的錯誤。 我試過Tim Golden的代碼:

import win32com.client session = win32com.client.gencache.EnsureDispatch ("MAPI.Session") # # Leave blank to be prompted for a session, or use # your own profile name if not "Outlook". It is also # possible to pull the default profile from the registry. # session.Logon ("Outlook") messages = session.Inbox.Messages # # Although the inbox_messages collection can be accessed # via getitem-style calls (inbox_messages[1] etc.) this # is the recommended approach from Microsoft since the # Inbox can mutate while you're iterating. # message = messages.GetFirst () while message: print message.Subject message = messages.GetNext ()

但是我得到一個錯誤:

pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)

不知道我的個人資料名稱是什么,所以我試著用:

session.Logon()

被提示,但沒有工作(同樣的錯誤)。 也嘗試用Outlook打開和closures,都沒有改變任何東西。

我有同樣的問題,你沒有發現很多工作。 下面的代碼就像一個魅力。

import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case, # the inbox. You can change that number to reference # any other folder messages = inbox.Items message = messages.GetLast() body_content = message.body print body_content

我已經創build了自己的迭代器來通過python遍歷Outlook對象。 問題是python嘗試從Index [0]開始進行迭代,但是Outlook期望第一個項目Index [1] …為了使Ruby更簡單,在下面的輔助類Oli中使用以下方法:

.items() – 產生一個元組(索引,Item)…

.prop() – 幫助反思Outlook對象公開可用的屬性(方法和屬性)

from win32com.client import constants from win32com.client.gencache import EnsureDispatch as Dispatch outlook = Dispatch("Outlook.Application") mapi = outlook.GetNamespace("MAPI") class Oli(): def __init__(self, outlook_object): self._obj = outlook_object def items(self): array_size = self._obj.Count for item_index in xrange(1,array_size+1): yield (item_index, self._obj[item_index]) def prop(self): return sorted( self._obj._prop_map_get_.keys() ) for inx, folder in Oli(mapi.Folders).items(): # iterate all Outlook folders (top level) print "-"*70 print folder.Name for inx,subfolder in Oli(folder.Folders).items(): print "(%i)" % inx, subfolder.Name,"=> ", subfolder

我遇到過同樣的問題。 結合從互聯網(和以上)的各種方法提出了以下方法(checkEmails.py)

class CheckMailer: def __init__(self, filename="LOG1.txt", mailbox="Mailbox - Another User Mailbox", folderindex=3): self.f = FileWriter(filename) self.outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI").Folders(mailbox) self.inbox = self.outlook.Folders(folderindex) def check(self): #=============================================================================== # for i in xrange(1,100): #Uncomment this section if index 3 does not work for you # try: # self.inbox = self.outlook.Folders(i) # "6" refers to the index of inbox for Default User Mailbox # print "%i %s" % (i,self.inbox) # "3" refers to the index of inbox for Another user's mailbox # except: # print "%i does not work"%i #=============================================================================== self.f.pl(time.strftime("%H:%M:%S")) tot = 0 messages = self.inbox.Items message = messages.GetFirst() while message: self.f.pl (message.Subject) message = messages.GetNext() tot += 1 self.f.pl("Total Messages found: %i" % tot) self.f.pl("-" * 80) self.f.flush() if __name__ == "__main__": mail = CheckMailer() for i in xrange(320): # this is 10.6 hours approximately mail.check() time.sleep(120.00)

為了一致性,我還包含了FileWriter類的代碼(可在FileWrapper.py中find)。 我需要這個,因為試圖pipe道UTF8在Windows中的文件無法正常工作。

class FileWriter(object): ''' convenient file wrapper for writing to files ''' def __init__(self, filename): ''' Constructor ''' self.file = open(filename, "w") def pl(self, a_string): str_uni = a_string.encode('utf-8') self.file.write(str_uni) self.file.write("\n") def flush(self): self.file.flush()

請享用。

檢查Outlook電子郵件更容易,如下所示,

`outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") folder = outlook.Folders[5] Subfldr = folder.Folders[5] messages_REACH = Subfldr.Items message = messages_REACH.GetFirst()`

在這里,我們可以將最多的第一封郵件放到郵箱中。

`outlook =win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") folder = outlook.Folders[5] Subfldr = folder.Folders[5] messages_REACH = Subfldr.Items message = messages_REACH.GetLast()`

有了這個,我們可以將最近的電子郵件收到郵箱中。

旁邊我們知道,我們需要調用,其他的細節,如主題,發件人,時間,郵箱郵件計數等。

總結

以上是生活随笔為你收集整理的python如何读取outlook邮件_通过MAPI使用Python从Outlook中阅读电子邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。