【问题标题】:Microsoft Graph, adding multiple attachments to an emailMicrosoft Graph,向电子邮件添加多个附件
【发布时间】:2019-05-05 06:21:46
【问题描述】:

我正在使用 .netCore 和 Microsoft Graph 并尝试将多个附件添加到电子邮件并发送。电子邮件发送得很好,一切都在那里(如果我发送 2 个附件,您会看到有 2 个附件),但是收件人只能打开第一个附件。 (所有附件都小于最大 4MB,所以这不是问题)。

代码是

string content = "{\"message\":{" +
                              "\"subject\":\"" + email.Subject + "\"," +
                               "\"body\":{" +
                                     "\"contentType\": \"HTML\"," +
                                     "\"content\": \"" + email.Msg + "\"" +
                            "}," +
                             "\"toRecipients\": [";
        foreach (var adr in email.SendTo)
        {
            content += "{\"emailAddress\": {\"address\": \"" + adr + "\"} },";
        }
        content += "]";
        if ( email.file != null ) // this is an collection of IFormFile 
        {
            List<EmailAttachment> emailAttachment = new List<EmailAttachment>();
            using (var memoryStream = new MemoryStream())
            {
                foreach (var elem in email.file)
                {
                    await elem.CopyToAsync(memoryStream);
                    emailAttachment.Add(new EmailAttachment
                    {
                        FileName = elem.FileName,
                        AttachmentFile = Convert.ToBase64String(memoryStream.ToArray()),
                        ContentType = elem.ContentType
                    });
                }
            }
            content += ", \"attachments\": [";  //, \"hasAttachments\": true
            emailAttachment.ForEach(elem =>
            {
                content += "{\"@odata.type\": \"#microsoft.graph.fileAttachment\"," +
                            "\"name\":\" " + elem.FileName + "\"," +
                            "\"contentType\":\" " + elem.ContentType +" \"," +
                            "\"contentBytes\":\" " + elem.AttachmentFile + "\"},";
            });
            content += "]";
        }
        content += " }}";

        StringContent contentString = new StringContent(content, Encoding.UTF8, "application/json");

我的代码的下一步是将此 httpContent 发送到 microsoft graph。

但是,问题是,如您所见,我尝试制作一系列附件以发送,但电子邮件的收件人只能打开一个附件(他确实看到了所有 3 个附件)。 (注意:我创建了一组收件人以立即发送,效果很好)。

感谢您的宝贵时间!

【问题讨论】:

    标签: c# asp.net-core-2.0 microsoft-graph-api msal


    【解决方案1】:

    几个小时后,答案是我使用一个内存流处理多个附件。它必须得到所有混淆并且不起作用。我现在将 foreach 移到内存流之外,一切正常。每个附件一个内存流。

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2011-02-03
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 2023-03-21
      • 2021-10-30
      • 1970-01-01
      • 2020-03-06
      相关资源
      最近更新 更多