让代码更简单

Microsoft Graph开放接口学习——获取邮件

重要:本文最后更新于2020-05-12 08:24:15,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

前面讲了Microsoft Graph开放接口学习系列教程的获取授权发送邮件教程,让大家初步体验到了Microsoft Graph开放接口的功能与使用方法。接下来我们继续介绍Microsoft Graph开放接口中关于邮件的相关接口,获取邮件接口。

获取登录用户的邮箱(包括“已删除邮件”和“待筛选邮件”文件夹)中的邮件。

根据页面大小和邮箱数据,从邮箱中获取邮件可能会引发多个请求。 默认页面大小为 10 封邮件。 若要获取下一页的邮件,只需将 @odata.nextLink 中返回的整个 URL 应用于下一个 get-messages 请求。 此 URL 包括可能已在初始请求中指定的任何查询参数。

不要尝试从 @odata.nextLink URL 中提取 $skip 值来操纵响应。 此 API 使用 $skip 值来保留其已在用户邮箱中遍历的所有项的计数,以返回 message-type 项的页面。 因此,甚至在初始响应中,$skip 值都会大于页面大小。

目前,此操作返回纯 HTML 格式的邮件正文。

在以下两种情况下,应用可以获取其他用户的邮件文件夹中的邮件:

  • 如果该应用具有应用程序权限,或者
  • 如果应用具有来自某个用户的相应委派权限,而另一个用户与该用户共享了邮件文件夹,或者已为该用户授予委派的访问权限。

相关权限

要调用此 API,需要以下权限之一。

表 1
权限类型 权限(从最低特权到最高特权)
委派(工作或学校帐户) Mail.ReadBasic、Mail.Read、Mail.ReadWrite
委派(个人 Microsoft 帐户) Mail.ReadBasic、Mail.Read、Mail.ReadWrite
应用程序 Mail.ReadBasic.All、Mail.Read、Mail.ReadWrite

接口介绍

请求方式:get

请求地址:https://graph.microsoft.com/v1.0/me/messages

请求头部

名称 类型 说明
Authorization string Bearer {token}。必需。
Prefer: outlook.body-content-type string 要返回的 body 和 uniqueBody 属性的格式。 可取值为“text”或“html”。 如果未指定此头,采用 HTML 格式返回 body 和 uniqueBody 属性。 可选。

返回值

如果成功,此方法在响应正文中返回 200 OK 响应代码和 Message 对象集合。

请求示例

请求如下地址,将返回包含发送者以及邮件主题相关内容的集合。

复制
https://graph.microsoft.com/v1.0/me/messages?$select=sender,subject

返回数据示例:

复制
{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('bb8775a4-4d8c-42cf-a1d4-4d58c2bb668f')/messages(sender,subject)",
    "@odata.nextLink": "https://graph.microsoft.com/v1.0/me/messages?$select=sender%2csubject&$skip=14",
    "value": [
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAwR4Hg\"",
            "id": "AAMkAGUAAAwTW09AAA=",
            "subject": "You have late tasks!",
            "sender": {
                "emailAddress": {
                    "name": "Microsoft Planner",
                    "address": "noreply@Planner.Office365.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4D1e\"",
            "id": "AAMkAGUAAAq5QKlAAA=",
            "subject": "You have late tasks!",
            "sender": {
                "emailAddress": {
                    "name": "Microsoft Planner",
                    "address": "noreply@Planner.Office365.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4D0v\"",
            "id": "AAMkAGUAAAq5QKkAAA=",
            "subject": "Your Azure AD Identity Protection Weekly Digest",
            "sender": {
                "emailAddress": {
                    "name": "Microsoft Azure",
                    "address": "azure-noreply@microsoft.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4DsN\"",
            "id": "AAMkAGUAAAq5QKjAAA=",
            "subject": "Use attached file",
            "sender": {
                "emailAddress": {
                    "name": "Megan Bowen",
                    "address": "MeganB@contoso.OnMicrosoft.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4Dq9\"",
            "id": "AAMkAGUAAAq5QKiAAA=",
            "subject": "Original invitation",
            "sender": {
                "emailAddress": {
                    "name": "Megan Bowen",
                    "address": "MeganB@contoso.OnMicrosoft.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4Dq1\"",
            "id": "AAMkAGUAAAq5QKhAAA=",
            "subject": "Koala image",
            "sender": {
                "emailAddress": {
                    "name": "Megan Bowen",
                    "address": "MeganB@contoso.OnMicrosoft.com"
                }
            }
        },
        {
            "@odata.etag": "W/\"CQAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4Dqp\"",
            "id": "AAMkAGUAAAq5QKgAAA=",
            "subject": "Sales invoice template",
            "sender": {
                "emailAddress": {
                    "name": "Megan Bowen",
                    "address": "MeganB@contoso.OnMicrosoft.com"
                }
            }
        },
        {
            "@odata.type": "#microsoft.graph.eventMessage",
            "@odata.etag": "W/\"DAAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4Dft\"",
            "id": "AAMkAGUAAAq5UMVAAA=",
            "subject": "Accepted: Review strategy for Q3",
            "sender": {
                "emailAddress": {
                    "name": "Adele Vance",
                    "address": "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=A17A02BCF30C4937A87B14273385667C-ADELEV"
                }
            }
        },
        {
            "@odata.type": "#microsoft.graph.eventMessage",
            "@odata.etag": "W/\"DAAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4DfF\"",
            "id": "AAMkAGUAAAq5UMUAAA=",
            "subject": "Accepted: Review strategy for Q3",
            "sender": {
                "emailAddress": {
                    "name": "Adele Vance",
                    "address": "/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=A17A02BCF30C4937A87B14273385667C-ADELEV"
                }
            }
        },
        {
            "@odata.type": "#microsoft.graph.eventMessage",
            "@odata.etag": "W/\"CwAAABYAAADHcgC8Hl9tRZ/hc1wEUs1TAAAq4Dfa\"",
            "id": "AAMkAGUAAAq5T8tAAA=",
            "subject": "Review strategy for Q3",
            "sender": {
                "emailAddress": {
                    "name": "Megan Bowen",
                    "address": "MeganB@contoso.OnMicrosoft.com"
                }
            }
        }
    ]
}

感觉很棒!可以赞赏支持我哟~

2 打赏

评论 (2)

登录后评论
不明觉厉
不明觉厉
QQ咨询 邮件咨询 狗哥推荐