获取邮件
命名空间:microsoft.graph
重要
Microsoft Graph /beta
版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
检索 消息 对象的属性和关系。
例如,可以获取消息并展开消息中的所有提及实例。 有关示例,请参阅 示例 2。
可使用 $value
参数来获取邮件的 MIME 内容。 有关示例,请参阅 示例 5。
应用可以在两个条件下在其他用户的邮件文件夹中获取邮件:
- 如果应用具有应用程序权限
- 如果应用具有来自一个用户的相应委派 权限 ,而另一个用户已与该用户共享邮件文件夹,或者已向该用户授予委派访问权限。 有关详细信息,请参阅 获取共享或委托文件夹中的 Outlook 邮件。
由于 消息 资源支持 扩展,因此还可以使用 GET
操作获取 消息 实例中的自定义属性和扩展数据。
此 API 可用于以下国家级云部署。
全局服务 | 美国政府 L4 | 美国政府 L5 (DOD) | 由世纪互联运营的中国 |
---|---|---|---|
✅ | ✅ | ✅ | ✅ |
权限
要调用此 API,需要以下权限之一。 若要了解详细信息,包括如何选择权限的信息,请参阅权限。
权限类型 | 权限(从最低特权到最高特权) |
---|---|
委派(工作或学校帐户) | Mail.ReadBasic、Mail.Read |
委派(个人 Microsoft 帐户) | Mail.ReadBasic、Mail.Read |
应用程序 | Mail.ReadBasic.All、Mail.Read |
HTTP 请求
要获取指定的邮件:
GET /me/messages/{id}
GET /users/{id | userPrincipalName}/messages/{id}
GET /me/mailFolders/{id}/messages/{id}
GET /users/{id | userPrincipalName}/mailFolders/{id}/messages/{id}
要获取指定邮件的 MIME 内容:
GET /me/messages/{id}/$value
GET /users/{id | userPrincipalName}/messages/{id}/$value
GET /me/mailFolders/{id}/messages/{id}/$value
GET /users/{id | userPrincipalName}/mailFolders/{id}/messages/{id}/$value
若要获取消息并展开消息中的所有提及,请执行以下命令:
GET /me/messages/{id}?$expand=mentions
GET /users/{id | userPrincipalName}/messages/{id}?$expand=mentions
GET /me/mailFolders/{id}/messages/{id}?$expand=mentions
GET /users/{id | userPrincipalName}/mailFolders/{id}/messages/{id}?$expand=mentions
可选的查询参数
此方法支持使用 OData 查询参数来帮助自定义响应。
使用 $value
参数获取邮件的 MIME 内容。
$expand
使用提及导航属性上的查询参数获取一条消息,其中包含展开消息中每个提及的详细信息。
请求标头
名称 | 类型 | 说明 |
---|---|---|
Authorization | string | 持有者 {token}。 必填。 |
Prefer: outlook.body-content-type | string | 要返回的 body 和 uniqueBody 属性的格式。 可取值为“text”或“html”。 如果指定此 Preference-Applied 头,返回 Prefer 头作为证明。 如果未指定此头,采用 HTML 格式返回 body 和 uniqueBody 属性。 可选。 |
请求正文
请勿提供此方法的请求正文。
响应
如果成功,此方法在响应正文中返回 200 OK
响应代码和 message 对象。
如果指定 $value
参数,则返回 MIME 格式的邮件内容,而不是邮件资源。
示例
示例1:获取特定邮件
请求
第一个示例获取指定的消息。 它不指定任何标头来指示要返回的正文的格式。
GET https://graph.microsoft.com/beta/me/messages/AAMkAGI1AAAoZCfHAAA=
响应
以下示例显示了相应的响应。 正文和 uniqueBody 属性以默认 HTML 格式返回。
注意:为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('cd209b0b-3f83-4c35-82d2-d88a61820480')/messages/$entity",
"@odata.etag":"W/\"CQAAABYAAABmWdbhEgBXTophjCWt81m9AAAoZYj4\"",
"id":"AAMkAGI1AAAoZCfHAAA=",
"subject":"Welcome to our group!",
"bodyPreview":"Welcome to our group, Dana! Hope you will enjoy working with us !\r\n",
"body":{
"contentType":"html",
"content":"<html>\r\n<head></head><body><p>Welcome to our group, Dana! Hope you will enjoy working with us </p></body></html>\r\n"
},
"uniqueBody":{
"contentType":"html",
"content":"<html>\r\n<head></head><body><p>Welcome to our group, Dana! Hope you will enjoy working with us </p></body></html>\r\n"
}
}
示例 2:获取特定消息中的所有提及
请求
在下一个示例中,已登录用户为 Dana Swope。 该示例显示获取 Dana 邮箱中指定邮件中所有提及的详细信息。
GET https://graph.microsoft.com/beta/me/messages/AQMkADJmMTUAAAgVZAAAA/?$expand=mentions
响应
以下示例显示了相应的响应。
注意:为了提高可读性,可能缩短了此处显示的响应对象。
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#me/messages/$entity",
"@odata.id":"https://graph.microsoft.com/beta/users('266efe5a-0fd7-4edd-877b-b2d1e561f193@ae01a323-3934-4475-a32d-af1274312bb0')/messages('AQMkADJmMTUAAAgVZAAAA')",
"@odata.etag":"W/\"CQAAABYAAAAPFhK2FclcRbABBJhCde8iAAAAAATI\"",
"id":"AQMkADJmMTUAAAgVZAAAA",
"subject":"Start planning soon",
"body":{
"contentType":"HTML",
"content":"<html><head></head><body><p><a href=\"mailto:danas@contoso.com\">@Dana Swope</a>,<a href=\"mailto:randiw@contoso.com\">@Randi Welch</a>, forgot to mention, I will be away this weekend. I can start on Monday though.</p></body></html>"
},
"bodyPreview":"@Dana Swope<mailto:danas@contoso.com>, @Randi Welch, forgot to mention, I will be away this weekend. I can start on Monday though.",
"sender":{
"emailAddress":{
"name":"Samantha Booth",
"address":"samanthab@contoso.com"
}
},
"from":{
"emailAddress":{
"name":"Samantha Booth",
"address":"samanthab@contoso.com"
}
},
"toRecipients":[
{
"emailAddress":{
"name":"Dana Swope",
"address":"danas@contoso.com"
}
},
{
"emailAddress":{
"name":"Randi Welch",
"address":"randiw@contoso.com"
}
}
],
"ccRecipients":[
],
"bccRecipients":[
],
"mentionsPreview":{
"isMentioned":true
},
"mentions":[
{
"@odata.id":"https://graph.microsoft.com/beta/users('266efe5a-0fd7-4edd-877b-b2d1e561f193@ae01a323-3934-4475-a32d-af1274312bb0')/messages('AQMkADJmMTUAAAgVZAAAA')/mentions('138f4c0a-1130-4776-b780-bf79d73abb3f')",
"id":"138f4c0a-1130-4776-b780-bf79d73abb3f",
"mentioned":{
"name":"Dana Swope",
"address":"danas@contoso.com"
},
"mentionText":null,
"clientReference":null,
"createdBy":{
"name":"Samantha Booth",
"address":"samanthab@contoso.com"
},
"createdDateTime":"2016-07-21T07:40:20.152Z",
"serverCreatedDateTime":"2016-07-21T07:40:20.152Z",
"deepLink":null,
"application":null
},
{
"@odata.id":"https://graph.microsoft.com/beta/users('266efe5a-0fd7-4edd-877b-b2d1e561f193@ae01a323-3934-4475-a32d-af1274312bb0')/messages('AQMkADJmMTUAAAgVZAAAA')/mentions('7b94df1a-0086-482a-b0da-e62fae12f983')",
"id":"7b94df1a-0086-482a-b0da-e62fae12f983",
"mentioned":{
"name":"Randi Welch",
"address":"randiw@contoso.com"
},
"mentionText":null,
"clientReference":null,
"createdBy":{
"name":"Samantha Booth",
"address":"samanthab@contoso.com"
},
"createdDateTime":"2016-07-21T07:40:20.158Z",
"serverCreatedDateTime":"2016-07-21T07:40:20.158Z",
"deepLink":null,
"application":null
}
]
}
示例 3:以文本格式获取邮件正文
请求
第三个示例介绍如何使用 Prefer: outlook.body-content-type="text"
标头获取采用文本格式的指定消息的 body 和 uniqueBody。
GET https://graph.microsoft.com/beta/me/messages/AAMkAGI1AAAoZCfHAAA=/?$select=subject,body,bodyPreview,uniqueBody
Prefer: outlook.body-content-type="text"
响应
以下示例显示了相应的响应。
注意: 响应包含一个
Preference-Applied: outlook.body-content-type
标头,用于确认Prefer: outlook.body-content-type
请求标头。
HTTP/1.1 200 OK
Content-type: application/json
Preference-Applied: outlook.body-content-type="text"
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('cd209b0b-3f83-4c35-82d2-d88a61820480')/messages(subject,body,bodyPreview,uniqueBody)/$entity",
"@odata.etag":"W/\"CQAAABYAAABmWdbhEgBXTophjCWt81m9AAAoZYj4\"",
"id":"AAMkAGI1AAAoZCfHAAA=",
"subject":"Welcome to our group!",
"bodyPreview":"Welcome to our group, Dana! Hope you will enjoy working with us !\r\n\r\nWould you like to choose a day for our orientation from the available times below:\r\n\r\n\r\nDate\r\n Time\r\n\r\nApril 14, 2017\r\n 1-3pm\r\n\r\nApril 21, 2017\r\n 10-12noon\r\n\r\n\r\n\r\nTh",
"body":{
"contentType":"text",
"content":"Welcome to our group, Dana! Hope you will enjoy working with us [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] !\r\n\r\nWould you like to choose a day for our orientation from the available times below:\r\n\r\n\r\nDate\r\n Time\r\n\r\nApril 14, 2017\r\n 1-3pm\r\n\r\nApril 21, 2017\r\n 10-12noon\r\n\r\n\r\n\r\nThanks!\r\n\r\n"
},
"uniqueBody":{
"contentType":"text",
"content":"Welcome to our group, Dana! Hope you will enjoy working with us [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] [\ud83d\ude0a] !\r\nWould you like to choose a day for our orientation from the available times below:\r\n\r\nDate\r\n Time\r\n\r\nApril 14, 2017\r\n 1-3pm\r\n\r\nApril 21, 2017\r\n 10-12noon\r\n\r\n\r\nThanks!\r\n"
}
}
示例 4:获取 Internet 邮件头
请求
第四个示例演示如何获取特定消息的 Internet 消息标头。
GET https://graph.microsoft.com/beta/me/messages/AAMkAGVmMDEz/?$select=internetMessageHeaders
响应
以下示例显示了相应的响应。
注意: 为简洁起见,响应对象中的 Internet 消息标头数已减少。
HTTP/1.1 200 OK
Content-type: application/json
{
"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('48d31887-5fad-4d73-a9f5-3c356e68a038')/messages(internetMessageHeaders)/$entity",
"@odata.type":"#microsoft.graph.eventMessageRequest",
"@odata.etag":"W/\"CwAAABYAAAAiIsqMbYjsT5e/T7KzowPTAAAa/qUB\"",
"id":"AAMkAGVmMDEz",
"internetMessageHeaders":[
{
"name":"Content-Type",
"value":"application/ms-tnef"
},
{
"name":"Content-Transfer-Encoding",
"value":"binary"
},
{
"name":"Subject",
"value":"Cloud and Mobile Working Group"
},
{
"name":"x-custom-header-group-name",
"value":"Washington"
},
{
"name":"x-custom-header-group-id",
"value":"WA001"
}
]
}
示例 5:获取 MIME 内容
请求
第五个示例获取已登录用户邮箱中邮件的 MIME 内容。
GET https://graph.microsoft.com/beta/me/messages/4aade2547798441eab5188a7a2436bc1/$value
响应
以下示例显示了相应的响应。 MIME 内容以 MIME-Version
标头开头。
HTTP/1.1 200 OK
Content-type: text/plain
Received: from contoso.com (10.194.241.197) by
contoso.com (10.194.241.197) with Microsoft
SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1374.0 via Mailbox
Transport; Mon, 4 Sep 2017 03:00:08 -0700
Received: from contoso.com (10.194.241.197) by
contoso.com (10.194.241.197) with Microsoft
SMTP Server (version=TLS1_2,
cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.1.1374.0; Mon, 4 Sep
2017 03:00:07 -0700
Received: from contoso.com
(fe80::5bf:5059:4ca0:5017) by contoso.com
(fe80::5bf:5059:4ca0:5017%12) with mapi id 15.01.1374.000; Mon, 4 Sep 2017
03:00:01 -0700
From: Administrator <admin@contoso.com>
To: Administrator <admin@contoso.com>
Subject: This email has attachment.
Thread-Topic: This email has attachment.
Thread-Index: AQHTJWSHSywMzSz8o0OJud48nG50GQ==
Date: Mon, 4 Sep 2017 10:00:00 +0000
Message-ID:
<4aade2547798441eab5188a7a2436bc1@contoso.com>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 04
X-MS-Exchange-Organization-AuthSource:
contoso.com
X-MS-Has-Attach: yes
X-MS-Exchange-Organization-Network-Message-Id:
0ffdb402-ec03-42c8-5d32-08d4f37bb517
X-MS-Exchange-Organization-SCL: -1
X-MS-TNEF-Correlator:
X-MS-Exchange-Organization-RecordReviewCfmType: 0
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="_004_4aade2547798441eab5188a7a2436bc1contoso_"
--_004_4aade2547798441eab5188a7a2436bc1contoso_
Content-Type: multipart/alternative;
boundary="_000_4aade2547798441eab5188a7a2436bc1contoso_"
--_000_4aade2547798441eab5188a7a2436bc1contoso_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
The attachment is an email.
--_000_4aade2547798441eab5188a7a2436bc1contoso_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"display:none;"><!-- P {margin-top:0;margi=
n-bottom:0;} --></style>
</head>
<body dir=3D"ltr">
<div id=3D"divtagdefaultwrapper" style=3D"font-size:12pt;color:#000000;font=
-family:Calibri,Helvetica,sans-serif;" dir=3D"ltr">
<p>The attachment is an email.</p>
</div>
</body>
</html>
--_000_4aade2547798441eab5188a7a2436bc1contoso_--
--_004_4aade2547798441eab5188a7a2436bc1contoso_
Content-Type: application/octet-stream; name="Attachment email.eml"
Content-Description: Attachment email.eml
Content-Disposition: attachment; filename="Attachment email.eml"; size=408;
creation-date="Mon, 04 Sep 2017 09:59:43 GMT";
modification-date="Mon, 04 Sep 2017 09:59:43 GMT"
Content-Transfer-Encoding: base64
RnJvbToJQWRtaW5pc3RyYXRvciA8YWRtaW5AdGVuYW50LUVYSEItMTQ3MS5jb20+DQpTZW50OglN
b25kYXksIFNlcHRlbWJlciA0LCAyMDE3IDM6MjYgUE0NClRvOglTcml2YXJkaGFuIEhlYmJhcg0K
U3ViamVjdDoJQXR0YWNobWVudCBlbWFpbA0KDQpJIHdpbGwgYXR0YWNoIHRoaXMgZW1haWwgdG8g
YW5vdGhlciBtYWlsLg0K
--_004_4aade2547798441eab5188a7a2436bc1contoso_--
相关内容
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈