首页 > 解决方案 > 在 Outlook Graph API 中,在草稿上调用 send 后,消息 ID 似乎发生了变化

问题描述

我正在与 Outlook365 集成,它需要跟踪通过应用程序发送的电子邮件回复,以便可以使用 Outlook 在线打开电子邮件。

因为/sendMail返回一个空响应,阅读文档我注意到可以创建对现有消息的草稿回复:

POST /v1.0/me/messages/{previous message ID}/createReply

这将返回表示回复的消息,该消息已经分配了消息 ID。使用正确的值更新消息后,我将其发送为:

POST /v1.0/me/messages/{draft message ID}/send

此调用返回一个空响应。尝试检索消息会导致未找到错误:

GET /v1.0/me/messages/{draft message ID}

但是,我在列出发送后的消息后注意到,消息被分配了一个新的 ID。

有没有办法关联两个 ID,或者以某种方式跟踪发送的消息 ID,以便我可以访问消息的正确(和最终)webLink属性?

首先十分感谢。

标签: microsoft-graph-api

解决方案


不幸的是,Microsoft-Graph-API在调用send-mail-API后没有返回sent-message-id

有一些解决方案可以找到 sent-message-id。

  1. 按互联网消息 ID 搜索
  2. 创建草稿,更新正文并发送消息(使用草稿消息 ID)
  3. 列出消息并过滤conversationId并获取最新消息。

解决方案 1 和 2 不稳定,Microsoft 没有提供可靠的文档,而且解决问题并不容易。

但是,第三种解决方案效果很好。

请考虑 Microsoft-Graph-API 不支持filterorderBy的组合。


图形包装器


// Microsoft-Graph-API built-in method for list-messages doesn't work fine, so we have to implement it
MGraph.geMessagesByUrl = async function(url) {
  const responseString = await request.get({
    url: url,
    headers: { Authorization: `Bearer ${this.tokens.access_token}` }
  })

  const data = JSON.parse(responseString)

  return data.value
}

MGraph.createMessage = async function(message) {
  // setup your microsoft-graph-api client

  return await client.api('/me/messages').post(message)
}

MGraph.updateMessage = async function(messageId, message) {
  // setup your microsoft-graph-api client

  return await client.api(`/me/messages/${messageId}`).update(message)
}

// Microsoft-Graph-API built-in method for add-attachments doesn't work fine, so we have to implement it
MGraph.addAttachmentNative = async function(messageId, attachment) {
  const options = {
    method: 'POST',
    url: `https://graph.microsoft.com/v1.0/me/messages/${messageId}/attachments`,
    headers: { 
      Authorization: `Bearer ${tokens.access_token}`,
      'Content-Type': 'application/json' 
    },
    body: JSON.stringify(attachment)
  }

  const responseString = await request.post(options)

  return JSON.parse(responseString)
}

MGraph.sendMessage = async function(messageId) {
  // setup your microsoft-graph-api client

  return await client.api(`/me/messages/${messageId}/send`).post({})
}


发件人

const sender = async function(email, attachments) {
  const createMessageResult = await MGraph.createMessage(email)

  for (const attachment of attachments) {
    await MGraph.addAttachmentNative(createMessageResult.id, attachment)
  }

  const updateMessageResult = await MGraph.updateMessage(createMessageResult.id, email.message)
  await MGraph.sendMessage(updateMessageResult.id)

  const conversationId = updateMessageResult.conversationId

  return conversationId
}


列出消息,按 conversatinId 过滤并获取发送的消息

const generateGetByConversationIdQuery = function (conversationId) {
  const syncMessagesPprojection = [
    'id', 'conversationId',
    'internetMessageHeaders', 'internetMessageId',
    'createdDateTime', 'lastModifiedDateTime',
    'sender', 'from', 'toRecipients', 'ccRecipients', 'bccRecipients',
    'hasAttachments', 'subject', 'isDraft', 'isRead'
    // 'bodyPreview', 'uniqueBody', 'body'
  ]

  const projection = syncMessagesPprojection.join(',')

  const select = `&$select=${projection}`
  const expand = '&$expand=attachments($select=id,name,contentType,size,isInline)'
  const filter = `$filter=conversationId eq '${conversationId}'`
  const top    = '&top=100'
  const url    = `https://graph.microsoft.com/v1.0/me/messages?${filter}${select}${expand}${top}`

  return url
}

// Setup you email and attachments objects

const conversationId = sender(email, attachments)
const url    = generateGetByConversationIdQuery(conversationId)
const result = await MGraph.geMessagesByUrl(url)

// Here is your sent message
const sentMessage = result[result.length - 1]

推荐阅读