Microsoft Graph移动邮件API遇Email ID含/字符报错问题
解决方案
问题根源在于编码方式:HttpUtility.UrlEncode会将"/"转为%2f,但Graph API会把该编码识别为路径分隔符,导致EmailId被拆分为多个路径段,最终触发资源找不到的错误。正确做法是使用URL安全的Base64编码处理EmailId。
修改后的代码:
using (var httpClient = new HttpClient()) { // 对EmailId执行URL安全的Base64编码 byte[] emailIdBytes = Encoding.UTF8.GetBytes(emailId); string base64Str = Convert.ToBase64String(emailIdBytes); // 替换Base64中的特殊字符,适配URL规则 string encodedEmailId = base64Str.Replace('+', '-').Replace('/', '_').TrimEnd('='); var request = new HttpRequestMessage(HttpMethod.Post, $"https://graph.microsoft.com/v1.0/me/messages/{encodedEmailId}/move"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); request.Content = new StringContent($"{{\"DestinationId\": \"{folderId}\"}}", Encoding.UTF8, "application/json"); var response = await httpClient.SendAsync(request); string errorMessage = await response.Content.ReadAsStringAsync(); }
原理说明
Graph API要求路径中的资源ID使用URL安全的Base64编码,这种编码通过替换+为-、/为_,并移除末尾的=,确保整个EmailId被当作一个完整的路径段,避免被服务器误解析。
内容的提问来源于stack exchange,提问作者Waqar
相关产品推荐
相关产品推荐

