• Azure Logic App (1) 使用Logic App通过Azure Graph API发送邮件


      《Windows Azure Platform 系列文章目录

      本文介绍如何使用Azure Logic App,通过Azure Graph发送邮件。

      首先我们需要准备:

      1.一个有效的Microsoft 365订阅

      2.一个有效的Azure订阅

      1.首先我们需要准备应用注册,具体创建过程可以参考:Azure AD (7) 应用程序和服务主体 (App and Service Principal)

      记录好应用ID, 应用秘钥和目录ID

      2.然后针对上面创建成功的应用注册,需要设置Microsoft Graph里的Mail Send权限,设置类型为Application Permission。如下图:

      

      3.创建1个新的Azure Logic App,我们这里设置为Consumption Plan (按消耗量计费),步骤略

      4.我们创建Logic App的设计思路如下所示:

      (1)用户在调用Logic App的时候,需要传入三个参数:邮件标题,邮件正文、邮件收件人

      (2)需要在Log App里保存三个值:应用ID, 应用秘钥和目录ID

      (3)首先通过应用ID, 应用秘钥和目录ID,访问Azure AD获得Access Token

      (4)通过Azure AD Access Token,调用Azure Graph API,推送邮件

      4.设置Log App Designer,如下图:

      

      

      5.When a HTTP request is received配置入下:

      主要传入三个参数:

      mailbody是邮件正文。

      mailreceiver是收件人

      mailsubject是邮件标题

      

      6.HTTP Get Bearer Token配置入下:

      

      7.Parse JSON的配置如下:

      

      8.发送邮件的逻辑如下:

      请注意修改下面的URI地址,在users后面的参数修改为具有发送邮件权限的Microsoft 365邮箱地址

      

      9.Azure Logic App的Code文件如下:

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "HTTP": {
                    "inputs": {
                        "body": {
                            "message": {
                                "body": {
                                    "content": "@variables('mailBody')",
                                    "contentType": "Text"
                                },
                                "subject": "@variables('mailSubject')",
                                "toRecipients": [
                                    {
                                        "emailAddress": {
                                            "address": "@variables('mailReceiver')"
                                        }
                                    }
                                ]
                            },
                            "saveToSendItems": "false"
                        },
                        "headers": {
                            "Authorization": "@{concat('Bearer ', body('Parse_JSON')?['access_token'])}",
                            "Content-Type": "application/json"
                        },
                        "method": "POST",
                        "uri": "https://microsoftgraph.chinacloudapi.cn/v1.0/users/admin@leizha01.partner.onmschina.cn/sendMail"
                    },
                    "runAfter": {
                        "Parse_JSON": [
                            "Succeeded"
                        ]
                    },
                    "type": "Http"
                },
                "HTTP_Get_Bearer_Token": {
                    "inputs": {
                        "body": "client_id=@{variables('clientId')}&scope=https://microsoftgraph.chinacloudapi.cn/.default&client_secret=@{variables('clientSecret')}&grant_type=client_credentials",
                        "headers": {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        "method": "POST",
                        "uri": "https://login.chinacloudapi.cn/@{variables('tenantId')}/oauth2/v2.0/token"
                    },
                    "runAfter": {
                        "Initialize_mail_Receiver": [
                            "Succeeded"
                        ]
                    },
                    "type": "Http"
                },
                "Initialize_clientId": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "clientId",
                                "type": "string",
                                "value": "这里是Client ID"
                            }
                        ]
                    },
                    "runAfter": {},
                    "type": "InitializeVariable"
                },
                "Initialize_clientSecret": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "clientSecret",
                                "type": "string",
                                "value": "这里是Client秘钥"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_clientId": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_mail_Body": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "mailBody",
                                "type": "string",
                                "value": "@triggerBody()?['mailbody']"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_mail_Subject": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_mail_Receiver": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "mailReceiver",
                                "type": "string",
                                "value": "@triggerBody()?['mailreceiver']"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_mail_Body": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_mail_Subject": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "mailsubject",
                                "type": "string",
                                "value": "@{triggerBody()?['mailsubject']}"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_tentantId": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Initialize_tentantId": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "tenantId",
                                "type": "string",
                                "value": "这里是tenandId"
                            }
                        ]
                    },
                    "runAfter": {
                        "Initialize_clientSecret": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "Parse_JSON": {
                    "inputs": {
                        "content": "@body('HTTP_Get_Bearer_Token')",
                        "schema": {
                            "properties": {
                                "access_token": {
                                    "type": "string"
                                },
                                "expires_in": {
                                    "type": "integer"
                                },
                                "ext_expires_in": {
                                    "type": "integer"
                                },
                                "token_type": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "runAfter": {
                        "HTTP_Get_Bearer_Token": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {
                            "mailbody": {
                                "type": "string"
                            },
                            "mailreceiver": {
                                "type": "string"
                            },
                            "mailsubject": {
                                "type": "string"
                            }
                        }
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {}
    }

      10.配置成功后,我们通过POSTMAN进行调用:

      

      

      11.调用成功后,我们就可以收到Logic App发送的邮件

      

      12.如果我们需要通过Azure Automation调用Logic App发送邮件,可以使用下面的PowerShell脚本:

    $Url = "这里输入Logic App的URL地址"
    
    
    $body = @{
        mailbody='This is Mail Body 2022-09-26'
        mailsubject='This is Mail Subject 2022-09-26'
        mailreceiver='leizha@microsoft.com'
        }
        
    Invoke-WebRequest -Method POST -Uri $url -Body ($body|ConvertTo-Json) -UseBasicParsing -ContentType 'application/json'
  • 相关阅读:
    索引虚拟oracle virtual index
    用户盘云存储——百度网盘
    数据类型泛型的嵌套使用
    函数返回值C语言中malloc函数返回值是否需要类型强制转换问题
    控制文件oracle controlfile structure
    程序语言POJ 2406 Power Strings
    数组信息[置顶] php数组转换js数组操作及json_encode应用
    代码电话《程序员的第一年》情感编
    泛型通配符泛型中使用 通配符
    数字字符串一道有道实习生笔试算法题分析
  • 原文地址:https://www.cnblogs.com/threestone/p/16731237.html
Copyright © 2020-2023  润新知