[英]How to save all attachments in email using for loop in Python and Outlook?
[英]How to save outlook email attachments with Python, on Virtal Desktop (VMWare)
下面的代碼適用於我的 W10 筆記本電腦,但不適用於我的 w10 或 w7 虛擬桌面 (VMWare)。
它讀取電子郵件並將來自特定發件人的附件保存到共享網絡驅動器,並將電子郵件移動到子文件夾。
我想我需要換行:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
任何指針都非常感謝。
import win32com.client
import re
path='//shared_drive/StatementFiles/'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
donebox = outlook.GetDefaultFolder(6).folders("Bank").folders("Statements")
messages = inbox.Items# get the first email
message = messages.GetLast()
sender_name = 'sender@email.com'
i = 1
while i < 100:
message = messages.GetLast()
try:
current_sender = str(message.Sender).lower()
current_subject = str(message.Subject)
messagedate = message.senton
if re.search(sender_name, current_sender) != None:
print(current_subject) # verify the subject
print(current_sender) # verify the sender
attachments = message.Attachments
attachment = attachments.Item(1)
attachment_name = str(attachment).lower()
attachment.SaveASFile(path + attachment_name)
att_path=path + attachment_name
message.UnRead = False
message.Move(donebox)
current_sender = str(message.Sender).lower()
current_subject = str(message.Subject)
print(f'Email moved.')
message = messages.GetNext()
except:
message = messages.GetNext()
i += 1
我沒有在虛擬桌面上安裝 Python,仍在等待它被打包,並且我沒有安裝權限,所以在虛擬桌面上我正在運行使用 PyInstaller 創建的 exe 文件 - 再次,這個 exe 在我的筆記本電腦上正常工作。 通過 cmd 提示運行此 exe 不會返回任何消息或錯誤。
奇怪的是,在虛擬桌面上運行它需要顛倒順序,所以不要使用 GetLast,而是使用 GetFirst 解決了這個問題。 確實遇到了另一個障礙,Outlook 會在 1、2、5 或 10 分鍾內顯示一條消息以允許程序發送電子郵件。 我找不到在我們的 VMWare 上停用它的方法,所以決定切換到 VBA,它現在正在做我需要的事情。 令人沮喪,但至少這是一個解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.