我利用 ms graph 创建了一系列方法,这些方法通常返回带有 id 的电子邮件对象或带有 id 的文件夹等。一些示例如下:
public async Task<Models.Email?> SendEmailAsync(string mailBox, AddEmailDTO email)
{
var requestBody = new Message
{
ToRecipients = email.To?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
CcRecipients = email.Cc?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
BccRecipients = email.Cc?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
Subject = email.Subject,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = email.Body,
}
};
var result = await _graphServiceClient.Users[mailBox].Messages.PostAsync(requestBody, rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
if(result != null)
{
await _graphServiceClient.Me.Messages[result.Id].Send.PostAsync(rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
return await GetEmailAsync(mailBox, result.Id!);
} else {
return null;
}
}
public async Task<Models.Email?> SendReplyToEmailAsync(string mailBox, string messageId, AddEmailDTO email)
{
var requestBody = new CreateReplyPostRequestBody
{
Message = new Message
{
ToRecipients = email.To?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
CcRecipients = email.Cc?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
BccRecipients = email.Cc?.Select(contact => new Recipient
{
EmailAddress = new Microsoft.Graph.Models.EmailAddress
{
Address = contact.EmailAddress,
Name = contact.Name
}
}).ToList(),
Subject = email.Subject,
Body = new ItemBody
{
ContentType = BodyType.Html,
Content = email.Body,
},
},
};
var result = await _graphServiceClient.Users[mailBox].Messages[messageId].CreateReply.PostAsync(requestBody, rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
if(result != null)
{
await _graphServiceClient.Users[mailBox].Messages[result.Id].Send.PostAsync( rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
return await GetEmailAsync(mailBox, result.Id!);
} else {
return null;
}
}
public async Task CopyEmailByFolderIdAsync(string mailBox, string messageId, string folderId)
{
var requestBody = new CopyPostRequestBody
{
DestinationId = folderId,
};
var result = await _graphServiceClient.Users[mailBox].Messages[messageId].Copy.PostAsync(requestBody, rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
}
public async Task MoveEmailByFolderIdAsync(string mailBox, string messageId, string folderId)
{
var requestBody = new MovePostRequestBody
{
DestinationId = folderId,
};
var result = await _graphServiceClient.Users[mailBox].Messages[messageId].Move.PostAsync(requestBody, rc =>
{
rc.Headers.Add("Prefer", "IdType=\"ImmutableId\"");
});
}
我担心的是,我稍后会使用这些 ID,例如,如果我发送一封电子邮件,然后将其移动到文件夹中。那么,我是否应该将 ImmutableId 标头添加到我发出的每个请求中,如这些示例所示?默认情况下这些不可变 id 是否存在,并且此标头只是将常用 id 替换为不可变 id?或者是否需要先使用不可变 ID 创建资源,以便稍后可以通过它访问?
那么我是否应该将 ImmutableId 标头添加到我发出的每个请求中,如这些示例所示?
如果您要存储和使用任何邮件 ID,那么您应该在所有请求中使用 ImmutableId,这样就不会有任何性能损失
默认情况下这些不可变 id 是否存在,并且此标头只是将常用 id 替换为不可变 id?或者是否需要先使用不可变 ID 创建资源,以便稍后可以通过它访问?
邮件在 Exchange 存储中的存储方式是该对象只有一个实例(每个邮箱),然后通常您获得的 id 将来自邮件所在容器(文件夹)的内容表。您请求 ImmutableId,您将获得一个标识符,API (Microsoft Graph) 可以使用该标识符来获取该项目,无论其容器位于哪个容器中,只要它仍在同一个邮箱中即可。有一个翻译 https://learn.microsoft.com/en-us/graph/api/user-translateexchangeids?view=graph-rest-1.0&tabs=http 操作,允许您将常规 id 转换为 ImmutableId一个,但只有在常规 ID 有效时才有效(即使 ImmutableId 本身不会改变),例如,一旦您移动一条消息,该常规 ID 就会从容器的内容文件夹中删除。