如何获取Outlook Webhook推送通知对应的目标邮箱地址?
获取Outlook推送通知对应的目标邮箱地址
推送通知的payload本身不会直接返回目标邮箱地址,你可以通过以下两种方法获取:
方法1:创建订阅时存入邮箱信息(推荐)
创建订阅时,利用clientState这个自定义字符串字段,把对应账户的邮箱地址或唯一标识存进去。比如创建订阅的请求示例:
{ "changeType": "updated", "notificationUrl": "你的接收通知的URL", "resource": "/me/mailFolders('inbox')/messages", "clientState": "target-user@example.com", // 直接存入目标邮箱地址 "expirationDateTime": "2024-12-31T00:00:00Z" }
收到推送通知时,直接读取clientState字段的值即可拿到目标邮箱,无需额外调用API。
方法2:通过Microsoft Graph API从邮件资源反推邮箱
如果创建订阅时未设置clientState,可以借助payload里的字段调用API获取:
- 提取
resourceData中的@odata.id值,格式通常为users/{userId}/messages/{messageId} - 调用Graph API获取邮件详情:
GET https://graph.microsoft.com/v1.0/users/{userId}/messages/{messageId} - 从上述请求的路径中提取
userId,再调用API获取用户信息:
返回结果里的GET https://graph.microsoft.com/v1.0/users/{userId}mail字段就是目标邮箱地址。
你也可以通过邮件的parentFolderId关联到收件箱所属邮箱:
- 调用
GET https://graph.microsoft.com/v1.0/users/{userId}/mailFolders/{parentFolderId} - 确认该文件夹为收件箱后,同样通过
userId调用用户信息API获取邮箱地址。
内容的提问来源于stack exchange,提问作者breakercode
相关产品推荐
相关产品推荐

