如何从Outlook中的其他帐户读取电子邮件

时间:2020-11-04 06:58:05

标签: python outlook

在我看来,我有2个帐户(帐户1和帐户2)。帐户1是默认帐户。

我正在尝试从python代码中读取帐户2中的电子邮件。我尝试了其他方法,但是没有用。

我发布了我的代码,从我的默认帐户中读取了我的代码。您能帮我阅读2号帐户的电子邮件吗?(不是默认值)

from win32com.client import Dispatch
import time
import datetime
import re
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = datetime.date.today()
 
sub_today = 'test4'
att_today = '20201018_2.xlsx'    
#att_today = re.match(regex_, )
for msg in all_inbox:
    if msg.Subject.find(sub_today) != -1 and msg.SentOn.date() == val_date:
        break

for att in msg.Attachments:
    if att.FileName == att_today:
        print(att_today)
        break

try:
    print(att.FileName)
    #att.SaveAsFile("C:/Users/Shwettha/Downloads/attachment/"+ att.FileName)
    att.SaveAsFile(os.path.join("D:\Script\Monitoring",att.FileName))
    print("SUCCESSFUL","Attachments Downloaded")
except:
    print("ERROR","Attachment Download Failed")   

 

1 个答案:

答案 0 :(得分:0)

尝试按以下步骤设置收件箱

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["0m3r@Email.com"].Folders["Inbox"]

或使用GetSharedDefaultFolder method (Outlook)

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Recip = olNs.CreateRecipient("0m3r@Email.com")
Recip.Resolve()
shared_inbox = olNs.GetSharedDefaultFolder(Recip, "6")

for Item in shared_inbox.Items:
    print(Item.Subject)