自动化将电子邮件附件发送到远程服务器的过程

问题描述 投票:0回答:2

在我们公司,我们每封邮件(Outlook)都会收到作为附件的 Excel 文件。这些 Excel 表必须手动复制到远程服务器 (Linux) 才能进行处理。我想完全自动化此过程(无需任何手动步骤),但这似乎并不容易,因为它必须在没有任何第三方工具的情况下完成。

到目前为止我的想法:

  1. 创建一个 VBA 脚本,将 Excel 保存到文件夹中,然后使用 powershell 脚本将其传输到远程服务器。该解决方案的问题在于必须打开 Outlook。由于客户端组件的原因,它不太实用。它必须在不启动任何客户端计算机的情况下工作。

  2. 使用 Power Automate 创建一个流程,将附件复制到 Onedrive,然后使用 bash 脚本以某种方式将文件复制到 Linux 服务器。但这似乎比我想象的要棘手一些,因为将文件从公司 Onedrive 下载到 Linux 机器并不那么容易。

有没有人已经做过类似的事情或者有更好的想法?

任何帮助/想法将不胜感激!

提前谢谢您!

linux vba powershell outlook email-attachments
2个回答
0
投票

您可以在 Outlook 中使用 VBA 从所选文件夹中提取所有附加文件。这是 VBA 代码的示例;

Sub PullAttachments()
    Dim outApp As Outlook.Application
    Dim outNames As Outlook.NameSpace
    Dim outFolder As Outlook.MAPIFolder
    Dim outMail As Outlook.MailItem
    Dim outAttachment As Outlook.Attachment
    Dim strPath As String
    Dim strFile As String
    
    ' The path where you want to save attached files <<<<<<
    strPath = "C:\Your\path\" 
    
    Set outApp = New Outlook.Application
    Set outNames = outApp.GetNamespace("MAPI")
    
    ' Select the Outlook folder to pull attached files
    Set outFolder = outNames.PickFolder
    
    ' Check if a folder is selected
    If Not outFolder Is Nothing Then

        For Each outMail In outFolder.Items
            ' Check if the email has attachments
            If outMail.Attachments.Count > 0 Then
                ' Loop through the attachments
                For Each outAttachment In outMail.Attachments
                    ' Check the attachment file type <<<<<<<<<
                    If Right(outAttachment.FileName, 4) = "xlsx" Then
                        ' Build the full file path
                        strFile = strPath & outAttachment.FileName
                        ' Save the attachment to the specified folder
                        outAttachment.SaveAsFile strFile
                    End If
                Next outAttachment
            End If
        Next outMail
    End If
    
    Set outAttachment = Nothing
    Set outMail = Nothing
    Set outFolder = Nothing
    Set outNames = Nothing
    Set outApp = Nothing
End Sub

0
投票

任何客户端解决方案都需要在系统上运行客户端应用程序才能运行代码。如果您使用 Office 365,您可以考虑创建一项服务,通过该服务您可以使用 MS Graph API 检索附加文件并将其上传到您的服务器。当然,如果您处理 Exchange Online。

如果是本地 Exchange,您可以考虑使用 EWS,请参阅EWS 应用程序和 Exchange 架构了解更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.