You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Graph 5.17(C#)添加附件发邮件抛出ODataError求助

Microsoft Graph 5.17 添加附件发送邮件时抛出ODataError异常

我在使用Microsoft Graph 5.17版本的C#代码发送邮件时,发现不添加附件可以正常发送,但添加附件后会抛出Microsoft.Graph.Models.ODataErrors.ODataError异常。以下是我的代码片段:

string tenantId = "";
string clientId = "";
string clientSecret = "";
string sMailboxUserObjectId = "";

var message = new Message();
List<Recipient> recLst = new List<Recipient>();
List<string> ToLst = new List<string>();
ToLst.Add("someone@gmail.com");
foreach (var recipient in ToLst)
{
    Recipient recObj = new Recipient();
    EmailAddress emailObj = new EmailAddress();
    emailObj.Address = recipient;
    recObj.EmailAddress = emailObj;
    recLst.Add(recObj);
}
message.ToRecipients = new List<Recipient>(recLst);

message.Body = new ItemBody
{
    //ContentType = BodyType.Html,
    Content = "Test mail, please ignore"
};

message.Subject = "Test Mail. ";
var att1 = new MG.Models.Attachment
{
    Name = "PoweredOffMachines_2023_7_18_9.csv",
    ContentType = "text/csv",
    OdataType = "#microsoft.graph.fileAttachment",
    Id = "PoweredOffMachines_2023_7_18_9",  
};

message.Attachments = new List<MG.Models.Attachment>();
message.HasAttachments = true;
message.Attachments.Add(att1);

var saveToSentItems = false;

SendMailPostRequestBody sendMailReqBody = new SendMailPostRequestBody();
sendMailReqBody.Message = message;
sendMailReqBody.SaveToSentItems = saveToSentItems;

try
{
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret);
    var graphClient = new MG.GraphServiceClient(clientSecretCredential, scopes);

    await graphClient
        .Users[sMailboxUserObjectId]
        .SendMail
        .PostAsync(sendMailReqBody);

}
catch (Exception ex)
{
    return "Failure";
}

问题原因

你的代码在创建fileAttachment时存在两个致命问题:

  1. 未提供附件内容:#microsoft.graph.fileAttachment类型必须包含文件的实际内容,要么通过ContentBytes传入Base64编码的字节数组,要么通过ContentUrl传入可访问的文件链接,缺少内容会导致Graph API拒绝请求并抛出ODataError。
  2. 手动设置附件Id:附件的Id是由Graph API自动生成的,不需要手动指定,手动设置可能引发数据冲突。

修复方案

修改附件创建部分,添加文件内容的Base64编码,并移除手动设置的Id属性:

// 读取本地文件并转换为Base64编码
string filePath = @"C:\Your\File\Path\PoweredOffMachines_2023_7_18_9.csv";
byte[] fileContent = File.ReadAllBytes(filePath);
string base64Content = Convert.ToBase64String(fileContent);

var att1 = new MG.Models.Attachment
{
    Name = "PoweredOffMachines_2023_7_18_9.csv",
    ContentType = "text/csv",
    OdataType = "#microsoft.graph.fileAttachment",
    ContentBytes = base64Content // 新增:传入文件的Base64内容
    // 移除手动设置的Id属性
};

额外注意事项

  • 确保应用拥有读取目标文件的权限,若文件来自网络存储,需保证能获取到字节内容。
  • 若使用ContentUrl替代ContentBytes,需确保该URL对Graph API是可访问的(例如公开链接或带有效凭证的链接)。
  • 确认应用已配置正确的权限:发送邮件需拥有Mail.Send应用权限(已在你的代码中使用.default范围,需确保权限已在Azure AD中授予)。

内容的提问来源于stack exchange,提问作者Venkata Praveen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 11:35:05