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!