首页 > 解决方案 > 如何让 Azure Logic 应用发送电子邮件

问题描述

我使用本教程创建了一个逻辑应用程序,只要资源发生更改,它就会通过我的 gmail 帐户向我的工作帐户发送电子邮件:

https://docs.microsoft.com/en-us/azure/event-grid/monitor-virtual-machine-changes-event-grid-logic-app

但是,出于某种原因,每当我更改虚拟机上的任何内容时,例如停止/启动它,什么都不会发生。当我尝试手动运行触发器时,它会失败并显示以下消息:

无法启动逻辑应用 newLogicApp 的运行。处理您的请求时出错。请稍后重试。

我所有的资源都在同一个订阅下的同一个资源组中。

资源组中没有任何应用服务计划。我不知道这是否需要,如果需要,如何将该应用服务计划链接到逻辑应用本身。

这是逻辑应用的代码视图:

{
    "$connections": {
        "value": {
            "azureeventgrid": {
                "connectionId": "/subscriptions/7137186b-8ebc-4bc6-952f-55efcb96d2f5/resourceGroups/testRG/providers/Microsoft.Web/connections/azureeventgrid-2",
                "connectionName": "azureeventgrid-2",
                "id": "/subscriptions/7137186b-8ebc-4bc6-952f-55efcb96d2f5/providers/Microsoft.Web/locations/eastus/managedApis/azureeventgrid"
            },
            "gmail_1": {
                "connectionId": "/subscriptions/7137186b-8ebc-4bc6-952f-55efcb96d2f5/resourceGroups/testRG/providers/Microsoft.Web/connections/gmail-1",
                "connectionName": "gmail-1",
                "id": "/subscriptions/7137186b-8ebc-4bc6-952f-55efcb96d2f5/providers/Microsoft.Web/locations/eastus/managedApis/gmail"
            }
        }
    },
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Condition": {
                "actions": {
                    "Send_email": {
                        "inputs": {
                            "body": {
                                "Body": "Resource: @{triggerBody()?['topic']}\n\nEvent Type: @{triggerBody()?['eventType']}\n\nEvent ID: @{triggerBody()?['id']}\n\nTime: @{triggerBody()?['eventTime']}",
                                "Subject": "Resource updated: @{triggerBody()?['subject']}",
                                "To": "xxx@yyy.com"
                            },
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['gmail_1']['connectionId']"
                                }
                            },
                            "method": "post",
                            "path": "/Mail"
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "expression": {
                    "and": [
                        {
                            "equals": [
                                "@triggerBody()?['data']['operationName']",
                                "Microsoft.Compute/virtualMachines/write"
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "If"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "When_a_resource_event_occurs": {
                "inputs": {
                    "body": {
                        "properties": {
                            "destination": {
                                "endpointType": "webhook",
                                "properties": {
                                    "endpointUrl": "@{listCallbackUrl()}"
                                }
                            },
                            "topic": "/subscriptions/7137186b-8ebc-4bc6-952f-55efcb96d2f5/resourceGroups/testRG"
                        }
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureeventgrid']['connectionId']"
                        }
                    },
                    "path": "/subscriptions/@{encodeURIComponent('7137186b-8ebc-4bc6-952f-55efcb96d2f5')}/providers/@{encodeURIComponent('Microsoft.Resources.ResourceGroups')}/resource/eventSubscriptions",
                    "queries": {
                        "x-ms-api-version": "2017-09-15-preview"
                    }
                },
                "splitOn": "@triggerBody()",
                "type": "ApiConnectionWebhook"
            }
        }
    }
}

标签: azureazure-logic-apps

解决方案


根据我的理解,本教程只是帮助我们如何监控虚拟机的变化,但不能监控虚拟机的启动/停止。我在 Azure 门户上对其进行了测试,如果启动/停止 VM,将不会触发逻辑应用。但是当我调整 VM 的大小时,逻辑被成功触发。你可以自己再测试一次。

顺便说一句,如果您想在 VM 启动时收到通知,您可以使用其他解决方案。转到您的虚拟机并单击“警报”->“新警报规则”。然后您可以添加“启动虚拟机”条件(另一个可能对您有帮助的条件是“创建或更新虚拟机”),并创建操作组以向自己发送电子邮件。(如下图所示)

在此处输入图像描述

更新:

看来(从您的查看代码)您的逻辑应用程序中的触发器(发生资源事件时)中没有添加“事件类型项”,您分享的教程中提到了它,您可以再次查看或参考我在下面发布的屏幕截图。

在此处输入图像描述

在此处输入图像描述

更新2:

在此处输入图像描述

在此处输入图像描述

这是逻辑应用程序的代码:

{
    "$connections": {
        "value": {
            "azureeventgrid": {
                "connectionId": "/subscriptions/*********/resourceGroups/hurylogicVM/providers/Microsoft.Web/connections/azureeventgrid",
                "connectionName": "azureeventgrid",
                "id": "/subscriptions/*********/providers/Microsoft.Web/locations/eastasia/managedApis/azureeventgrid"
            },
            "office365": {
                "connectionId": "/subscriptions/*********/resourceGroups/hurylogicVM/providers/Microsoft.Web/connections/office365",
                "connectionName": "office365",
                "id": "/subscriptions/*********/providers/Microsoft.Web/locations/eastasia/managedApis/office365"
            }
        }
    },
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "If_a_virtual_machine_in_your_resource_group_has_changed": {
                "actions": {
                    "Send_email_when_virtual_machine_updated": {
                        "inputs": {
                            "body": {
                                "Body": "Resource: @{triggerBody()?['topic']}\nEvent type: @{triggerBody()?['eventType']}\nEvent id: @{triggerBody()?['id']}\nEvent time: @{triggerBody()?['eventTime']}",
                                "Subject": "Resource updated:@{triggerBody()?['subject']}",
                                "To": "v-shuhsh@microsoft.com"
                            },
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['office365']['connectionId']"
                                }
                            },
                            "method": "post",
                            "path": "/Mail"
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "expression": {
                    "and": [
                        {
                            "equals": [
                                "@triggerBody()?['data']['operationName']",
                                "Microsoft.Compute/virtualMachines/write"
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "If"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "When_a_resource_event_occurs": {
                "inputs": {
                    "body": {
                        "properties": {
                            "destination": {
                                "endpointType": "webhook",
                                "properties": {
                                    "endpointUrl": "@{listCallbackUrl()}"
                                }
                            },
                            "filter": {
                                "includedEventTypes": [
                                    "Microsoft.Resources.ResourceActionSuccess",
                                    "Microsoft.Resources.ResourceDeleteSuccess",
                                    "Microsoft.Resources.ResourceWriteSuccess"
                                ]
                            },
                            "topic": "/subscriptions/*********/resourceGroups/hurylogicVM"
                        }
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureeventgrid']['connectionId']"
                        }
                    },
                    "path": "/subscriptions/@{encodeURIComponent('*********')}/providers/@{encodeURIComponent('Microsoft.Resources.ResourceGroups')}/resource/eventSubscriptions",
                    "queries": {
                        "x-ms-api-version": "2017-09-15-preview"
                    }
                },
                "splitOn": "@triggerBody()",
                "type": "ApiConnectionWebhook"
            }
        }
    }
}

我的资源组截图:

在此处输入图像描述


推荐阅读