![](/img/trans.png)
[英]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.