使用node-ews v3.5.0标记邮件为已读时遇内部服务器错误求助
解决node-ews标记Exchange邮件为已读时的内部服务器错误
使用node-ews 3.5.0版本调用UpdateItem操作标记邮件为已读时,返回如下内部服务器错误:
{ "ResponseMessages":{ "UpdateItemResponseMessage":{ "attributes":{ "ResponseClass":"Error" }, "MessageText":"An internal server error occurred. The operation failed., Object reference not set to an instance of an object.", "ResponseCode":"ErrorInternalServerError", "DescriptiveLinkKey":0, "Items":null } } }
问题原因
代码中SetItemField的SOAP结构定义错误,将Message节点嵌套在了FieldURI内部,不符合EWS的规范要求。EWS要求SetItemField下需同时包含FieldURI和对应的项目节点(此处为Message),二者是同级关系,而非嵌套结构。
修正后的代码
const markFolderAsRead = async (ews, id, changeKey) => { const args = { attributes: { MessageDisposition: "SaveOnly", ConflictResolution: "AlwaysOverwrite" // 添加冲突解决策略,避免版本冲突报错 }, ItemChanges: { ItemChange: { ItemId: { attributes: { Id: id, ChangeKey: changeKey, }, }, Updates: { SetItemField: [ { FieldURI: { attributes: { FieldURI: "message:IsRead", }, }, Message: { IsRead: true, }, } ] }, }, }, }; try { const result = await ews.run("UpdateItem", args); console.log("email read:", JSON.stringify(result)); } catch (err) { console.error("Error marking email as read:", err); } };
关键修改说明
- 调整
SetItemField结构:将Message节点从FieldURI中移出,与FieldURI同级,匹配EWS的SOAP结构要求 - 添加
ConflictResolution属性:指定冲突解决策略,避免因邮件版本变更导致的操作失败 - 改用
try/catch替代.then(),更贴合async/await的写法,便于异常捕获
内容的提问来源于stack exchange,提问作者Victor Bello
相关产品推荐
相关产品推荐

