Skip to content

ItemAttachment doesn't preserve the Attachments inside it #2876

@VenkateshDharavath

Description

@VenkateshDharavath

Describe the bug

I'm trying to send an email A which has another email B attached to it as an ItemAttachment. Also, email B has a file attachments in it. The target user receives an email A with an ItemAttachment email B in it but the file attachment inside it goes missing.

Expected behavior

The target user should receive an email A with an ItemAttachment email B that has a file attachment in it.

How to reproduce

private Message PrepareMessage(Email email, bool isHtml, string inputFolderPath)
{
    var from = _configuration["FromAccount"];
    var subject = email.Subject + "_" + _sessionId;
    var to = _configuration["TestInbox"];

    var message = new Message();
    message.ToRecipients = new List<Recipient> { new Recipient { EmailAddress = new EmailAddress { Address = to }, OdataType = "#microsoft.graph.recipient" } };
    message.From = new Recipient { EmailAddress = new EmailAddress { Address = from }, OdataType = "#microsoft.graph.recipient" };
    message.Subject = subject;
    message.Body = new ItemBody { Content = email.Body, ContentType = isHtml ? BodyType.Html : BodyType.Text, OdataType = "#microsoft.graph.itemBody" };

    if (message.Attachments == null)
        message.Attachments = new List<Attachment>();

    foreach (var attachmentFile in email.Attachments)
    {
        var attachmentPath = Path.Combine(inputFolderPath, "Attachments", attachmentFile);

        var attachment = new FileAttachment
        {
            OdataType = "#microsoft.graph.fileAttachment",
            Name = attachmentFile,
            ContentBytes = File.ReadAllBytes(attachmentPath),
            ContentType = MimeTypeHelper.GetMimeType(attachmentFile),
        };

        message.Attachments.Add(attachment);
    }

    return message;
}

private Message PrepareParentMessage(Email email, Message message)
{
    var parentMessage = new Message
    {
        ToRecipients = message.ToRecipients?.Select(r => new Recipient { EmailAddress = new EmailAddress { Address = r.EmailAddress?.Address }, OdataType = "#microsoft.graph.recipient" }).ToList(),
        From = new Recipient { EmailAddress = new EmailAddress { Address = message.From?.EmailAddress?.Address }, OdataType = "#microsoft.graph.recipient" },
        Subject = message.Subject,
        Attachments = new List<Attachment>
        {
            new ItemAttachment
            {
                Name = PrepareItemAttachmentName(email.Subject), // The subject now is the format of subject_sessionId
                Item = message,
                IsInline = false,
                OdataType = "#microsoft.graph.itemAttachment"
            }
        },
        OdataType = "#microsoft.graph.message"
    };

    return parentMessage;
}

First, I'm creating a message/email B by calling PrepareMessage then calling PrepareParentMessage to create message/email A. Sending the message as below.

var messagePayload = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
    Message = messageA, // has an ItemAttachment messageB that has a file attachment inside it.
    SaveToSentItems = true,
};

await graphClient.Users[configuration["FromAccount"]].SendMail.PostAsync(messagePayload);

SDK Version

5.74.0

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```
</details>


### Configuration

- Os: Microsoft Windows 10

### Other information

_No response_

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions