Modify attachments of forwarded message with Graph SDK for C#

Forwarding message

Send forward message

The Graph API allows to directly send a forward message where you can specify either a comment of the body of the message.

Specifying comment:

var body = new Microsoft.Graph.Me.Messages.Item.Forward.ForwardPostRequestBody
{
    Comment = "Just do it",
    ToRecipients = new List<Recipient>
    {
        new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "john.doe@contoso.com"
            }
        }
    }
};
await graphClient.Me.Messages["{message_id}"].Forward.PostAsync(body);

Specifying message:

var body = new Microsoft.Graph.Me.Messages.Item.Forward.ForwardPostRequestBody
{
    Message = new Message
    {
        Importance = Importance.High,
        Body = new ItemBody
        {
            ContentType = BodyType.Text,
            Content = "Just do it"
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "john.doe@contoso.com"
                }
            }
        }
    }
};
await graphClient.Me.Messages["{message_id}"].Forward.PostAsync(body);

When forward message is sent directly, it's not possible to add or delete an attachment of the forward message.

Create a draft to send forward message later

The Graph API provide an alternative to create a draft of the forward message, and send it later. When a draft is created, we can modify attachments.

Create draft for forward:

var requestBody = new Microsoft.Graph.Me.Messages.Item.CreateForward.CreateForwardPostRequestBody
{
    Message = new Message
    {
        Importance = Importance.High,
        Body = new ItemBody
        {
            ContentType = BodyType.Text,
            Content = "Just do it"
        },
        ToRecipients = new List<Recipient>
        {
            new Recipient
            {
                EmailAddress = new EmailAddress
                {
                    Address = "john.doe@contoso.com"
                }
            }
        }
    }
};
var result = await graphClient.Me.Messages["{message-id}"].CreateForward.PostAsync(requestBody);

Use message id of the draft to add or delete attachments

Retrieve attachments:

var attachments = await graphClient.Me.Messages["{forward-draft-message-id}"].Attachments.GetAsync();

Add attachment

var fileAttachment = new FileAttachment
{
    Name = "new attachment",
    ContentBytes = Encoding.UTF8.GetBytes(Convert.ToBase64String(File.ReadAllBytes("{file_path}"))
};
await graphClient.Me.Messages["{forward-draft-message-id}"].Attachments.PostAsync(fileAttachment);

Delete attachment

await graphClient.Me.Messages["{forward-draft-message-id}"].Attachments["{attachment-id}"].DeleteAsync();

Send the draft

await graphClient.Me.Messages["{forward-draft-message-id}"].Send.PostAsync();
2
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x