Microsoft Graph,将多个附件添加到电子邮件 [英] Microsoft Graph, adding multiple attachments to an email

查看:82
本文介绍了Microsoft Graph,将多个附件添加到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

代码是

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图形.

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

但是,问题是,正如您所看到的,我尝试制作一系列要发送的附件,但是电子邮件的收件人只能打开一个附件(他确实看到全部3个).(请注意:我安排了一系列收件人来一次发送,效果很好).

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).

感谢您的时间!

推荐答案

很多小时后,答案是我将一个内存流用于多个附件.它必须弄混了所有,并且不起作用.现在,我将foreach移到了内存流之外,并且一切正常.每个附件一个内存流.

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.

这篇关于Microsoft Graph,将多个附件添加到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆