我正在调用 Gmail API 端点:
https://gmail.googleapis.com/gmail/v1/users/me/messages/[id]
以获取电子邮件(其中 [id]
是我收到的消息的 ID)。
我知道我的 OAuth 和 API 调用正在运行,因为我可以在我的应用程序中成功显示基本电子邮件信息(主题、片段等)。然而,我正在努力解析正文的 MIME 数据。
我正在使用MimeKit,并已审查并实施了解析消息的文档。我的代码看起来像这样:
public static string ParseMessage(string content)
{
// 'content' contains the json returned from the Gmail API call.
JsonNode jsonObject = JsonNode.Parse(content);
// ... parsing other stuff here that works ...
// Parsing the MIME content that does NOT work...
string payload = jsonObject["payload"].ToJsonString();
string body = ParseMime(payload);
// ... other stuff ...
}
public static string ParseMime(string content)
{
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(content));
MimeMessage message = MimeMessage.Load(stream);
// Implementation from 'Working with Messages' goes here ...
// http://www.mimekit.net/docs/html/Working-With-Messages.htm
}
我尝试实现文档的使用消息页面中的三种策略中的每一种,但它们都没有按预期工作。检查
MimeMessage
中的数据结构,我发现它们是空的。这让我相信内容没有被正确解析。
MimeMessage.Load()
函数解析的正确内容? 根据 API 方法文档 和 response REST 资源文档,我相信 payload
属性中的 json 对象响应就是所需要的(MIME 部分树的根)。我从 Gmail API 收到的响应(仅供参考)如下所示(实际响应中的数据值不为空,但出于隐私原因在此处删除):
{
"id": "",
"threadId": "",
"labelIds": [],
"snippit": "",
"payload": {
"partId": "",
"mimeType": "",
"filename": "",
"headers": [],
"body": {},
"parts": []
},
"sizeEstimate": ###,
"historyId": "",
"internalDate": ""
}
您粘贴的示例 JSON 中的“payload”属性不包含 MIME 消息,它包含一个 JSON 字典,而 MimeKit 无法解析该字典。
您需要找到一种方法来获取原始 MIME 消息数据。