简体   繁体   English

如何在虚拟桌面 (VMWare) 上使用 Python 保存 Outlook 电子邮件附件

[英]How to save outlook email attachments with Python, on Virtal Desktop (VMWare)

The below code works on my W10 laptop, but not on my w10 or w7 virtual desktop (VMWare).下面的代码适用于我的 W10 笔记本电脑,但不适用于我的 w10 或 w7 虚拟桌面 (VMWare)。

It reads emails and save attachments from a specific sender to a shared network drive, and moves the email to a sub folder.它读取电子邮件并将来自特定发件人的附件保存到共享网络驱动器,并将电子邮件移动到子文件夹。

I guess I need to change the line:我想我需要换行:

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

any pointers much appreciated.任何指针都非常感谢。

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

I don't have Python installed on eiter of the virtual desktops, still waiting for it to be packaged, and I don't have the permissions to install, so on the virtual desktops I am running an exe file created with PyInstaller - again, this exe works correctly on my laptop.我没有在虚拟桌面上安装 Python,仍在等待它被打包,并且我没有安装权限,所以在虚拟桌面上我正在运行使用 PyInstaller 创建的 exe 文件 - 再次,这个 exe 在我的笔记本电脑上正常工作。 Running this exe via cmd prompts returns no message or error.通过 cmd 提示运行此 exe 不会返回任何消息或错误。

Oddly running this on a virtual desktop you need to reverse the order, so instead of going for GetLast, using GetFirst resolved this.奇怪的是,在虚拟桌面上运行它需要颠倒顺序,所以不要使用 GetLast,而是使用 GetFirst 解决了这个问题。 Did hit another snag where Outlook brings up a message to allow an programme to emails, of either 1, 2, 5 or 10 minutes.确实遇到了另一个障碍,Outlook 会在 1、2、5 或 10 分钟内显示一条消息以允许程序发送电子邮件。 I couldn't find a way to deactivate this on our VMWare, so have decided to switch to VBA, which is now doing what I need.我找不到在我们的 VMWare 上停用它的方法,所以决定切换到 VBA,它现在正在做我需要的事情。 Frustrating, but at least it's a solution.令人沮丧,但至少这是一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM