TheLethalCoder
TheLethalCoder

Reputation: 6744

Add header to message with Graph API

I'm currently using Graph API to download messages and then move them to a different mail folder. This all works perfectly. As part of this process though we need to add a custom header to the email and then we can track it easier to see where it ends up. We used to do all of this using Mime/MailKit, however, this does not yet support the Graph API and so non-interactive OAuth2.0.

I tried adding headers to the message, uploading that to the new mail folder and then deleting the original message. However, it looks like doing it this way means we lose information (attachments, other body types, headers, etc.).

var messageHeaders = new System.Collections.Generic.List<InternetMessageHeader>();
if (message.InternetMessageHeaders != null)
{
    messageHeaders.AddRange(message.InternetMessageHeaders);
}

InternetMessageHeader header = messageHeaders.FirstOrDefault(h => h.Name.Equals(HEADER_ID));
if (header != null)
{
    header.Value = headerValue;
}
else
{
    messageHeaders.Add(new InternetMessageHeader()
    {
        Name = HEADER_ID,
        Value = headerValue
    });
}

message.InternetMessageHeaders = messageHeaders;

Message uploadedMessage = await UploadMessageAsync(graphServiceClient, user, destinationMailFolder, message);

await DeleteMessageAsync(graphServiceClient, user, message);

static async Task<Message> UploadMessageAsync(GraphServiceClient graphServiceClient, User user, MailFolder mailFolder, Message message)
    => await graphServiceClient.Users[user.Id].MailFolders[mailFolder.Id].Messages.Request().AddAsync(message);

static async Task DeleteMessageAsync(GraphServiceClient graphServiceClient, User user, Message message)
{
    await graphServiceClient.Users[user.Id].Messages[message.Id].Request().DeleteAsync();
}

I then wanted to try doing it a different way where I would download the MIME content of the message, add the header and then re-upload the message. I was able to easily do the first part of this. However, I can't work out a way to upload a message from file/stream using the Graph API.

Lastly, I thought updating the message might be the way to go here but according to the documentation it doesn't look like you can alter/add headers this way.

How can I do this?

Upvotes: 0

Views: 2091

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142252

I believe the MIME support is read-only currently. To move emails, I suggest using the move action:

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var destinationId = "deleteditems";

await graphClient.Me.Messages["{message-id}"]
    .Move(destinationId)
    .Request()
    .PostAsync();

https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http

Unfortunately, the internetMessageHeader property is currently readOnly. I would recommend filing a request aka.ms/graphrequest to add support for writing these headers.

Upvotes: 1

Related Questions