Skip to content

ItemAttachment doesn't preserve the Attachments inside it #2876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
VenkateshDharavath opened this issue Mar 26, 2025 · 0 comments
Open

ItemAttachment doesn't preserve the Attachments inside it #2876

VenkateshDharavath opened this issue Mar 26, 2025 · 0 comments
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience

Comments

@VenkateshDharavath
Copy link

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_
@VenkateshDharavath VenkateshDharavath added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience
Projects
None yet
Development

No branches or pull requests

1 participant