【Azure Logic App】Workflow中使用 SMTP 发送邮件成功,但打开附件时报错文件已损坏 - LuBu0505/My-Code GitHub Wiki

问题描述

在 Azure Logic Apps Standard 中,可以通过 Azure Blob Storage Connector 获取文件内容,再通过内置 SMTP Send Email Action 将文件作为邮件附件发送。

但是,在实际运行时候,遇见了问题:

1: Logic App Workflow 本身能够运行

2: SMTP 邮件能够成功发送

3: 邮件中也存在附件

4: 但下载并打开附件时,提示文件损坏,无法正常打开 image.png

基于以上的情况,应该如何解决问题呢?

问题解答

在只使用SMTP Send_Email Action做一个简单复现实验,发现邮件发送成功,并且附件正常打开。

随后检查 Logic App 中 Workflow配置,发现实际配置中的 contentData 值包含了 { 和 }。

使用相同形式可以复现问题,邮件中的附件无法打开,提示内容被破坏。

因此,该问题的错误点是:SMTP Send_Email  Action 的 contentData 参数中出现了额外的 { },修改后问题得到解决。

错误的附件配置为:

"attachment": [
    {
        "fileName": "test.xlsm",
        "contentData": "@{body('Get_blob_content')}",
        "contentType": "xlsm"
    }
 ]

正确的附件配置如下:

"attachment": [
  {
    "fileName": "test.xlsm",
    "contentData": "@body('Get_blob_content')",
    "contentType": "xlsm"
  }
]

其中:fileName 指定附件名称,contentData 使用 Get_blob_content Action 返回的文件内容,contentType 设置为 xlsm。

附件一:完整的Logic App workflow code如下

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "contentVersion": "1.0.0.0",
        "actions": {
            "Get_blob_content_(V2)": {
                "type": "ApiConnection",
                "inputs": {
                    "host": {
                        "connection": {
                            "referenceName": "xxxblob"
                        }
                    },
                    "method": "get",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent('xxxxxx'))}/content",
                    "queries": {
                        "inferContentType": true
                    }
                },
                "runAfter": {},
                "metadata": {
                    "xxxxx": "test.xlsm"
                }
            },
            "Send_Email": {
                "type": "ServiceProvider",
                "inputs": {
                    "parameters": {
                        "from": "[email protected]",
                        "to": "[email protected]",
                        "importance": "Normal",
                        "attachment": [
                            {
                                "fileName": "test.xlsm",
                                "contentData": "@body('Get_blob_content')",
                                "contentType": "xlsm"
                            }
                        ]
                    },
                    "serviceProviderConfiguration": {
                        "connectionName": "Smtp",
                        "operationId": "sendEmail",
                        "serviceProviderId": "/serviceProviders/Smtp"
                    }
                },
                "runAfter": {
                    "Get_blob_content": [
                        "SUCCEEDED"
                    ]
                }
            }
        },
        "outputs": {},
        "triggers": {
            "When_an_HTTP_request_is_received": {
                "type": "Request",
                "kind": "Http"
            }
        }
    },
    "kind": "Stateful"
}

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!