Microsoft Graph, adding multiple attachments to an email

↘锁芯ラ 提交于 2021-01-29 08:26:35

问题


I am using .netCore and Microsoft Graph and trying to add multiple attachments to an email and send it. The email sends nicely, all is there (if i send 2 attachments you see that there are 2 attachments), however only the first attachment is able to be opened up by the recipient. (the attachments all together are less than the 4MB max, so that is not the issue).

The code is

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");

The next step of my code is sending this httpContent to microsoft graph.

However, The problem is, as you can see I try making an array of attachments to send, but the recipient of the email only can open one attachment (he does see all 3). (Note: I made an array of recipients to send at once and that works beautifully).

Thanks for your time!


回答1:


after many hours, the answer is I was using one memory stream for multiple attachments. it must of gotten all mixed up and doesn't work. I now moved the foreach outside of the memory stream and it all works. One memory stream per attachment.



来源:https://stackoverflow.com/questions/53602529/microsoft-graph-adding-multiple-attachments-to-an-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!