基于Microsoft Graph API v1追踪Outlook邮件回复耗时的技术问询
用Microsoft Graph API v1统计邮件回复耗时方案
1. 筛选收件邮件
调用/me/messages接口时,通过$filter参数精准过滤出收件邮件,排除自己发送的邮件和草稿:
GET https://graph.microsoft.com/v1.0/me/messages?$filter=isDraft eq false and sender/emailAddress/address ne '{你的邮箱地址}'&$select=id,receivedDateTime,conversationId,subject
isDraft eq false:排除未发送的草稿邮件sender/emailAddress/address ne '{你的邮箱}':排除自己发出的邮件,只保留他人发送给你的收件邮件$select:只请求需要的字段,减少返回数据体积,提升接口响应速度
2. 获取对应回复时间
每封收件邮件的conversationId会关联同一对话下的所有邮件,通过这个ID可以找到你发送的回复邮件,进而拿到回复时间:
GET https://graph.microsoft.com/v1.0/me/messages?$filter=conversationId eq '{原邮件的conversationId}' and sender/emailAddress/address eq '{你的邮箱地址}'&$select=sentDateTime&$orderby=sentDateTime asc&$top=1
conversationId eq '{原邮件ID}':限定在同一邮件对话中查找sender/emailAddress/address eq '{你的邮箱}':确保找到的是你发送的回复邮件$orderby=sentDateTime asc&$top=1:取最早的回复时间(如果有多轮回复,按需求可改为取最后一封,调整$orderby为desc即可)
3. 计算回复耗时
拿到原邮件的receivedDateTime(收件时间)和回复邮件的sentDateTime(回复时间)后,将两个时间字符串转为时间对象,直接计算差值即可。示例代码(JavaScript):
// 原邮件收件时间 const receivedTime = new Date(originalMail.receivedDateTime); // 回复邮件发送时间 const replyTime = new Date(replyMail.sentDateTime); // 计算时间差(毫秒) const timeDiff = replyTime - receivedTime; // 转换为小时和分钟 const hours = Math.floor(timeDiff / (1000 * 60 * 60)); const minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60)); console.log(`回复耗时:${hours}小时${minutes}分钟`);
关键注意点
- 确保你的应用已获取
Mail.Read权限(根据应用类型选择委托权限或应用权限) - 需处理无回复的邮件场景(此时查询回复邮件会返回空数组,要做判空处理)
- 如果需要批量处理邮件,可使用
$top参数分页获取,避免单次请求数据量过大
内容的提问来源于stack exchange,提问作者Harish Ksoft
相关产品推荐
相关产品推荐

