DEV Community

Glen Scales
Glen Scales

Posted on

How to upload an Email using the Microsoft Graph API into the Inbox

Sometimes you may need to create an Email from another system and while the Microsoft Graph doesn't yet provide any Mail Migration API's this method can be use to achieve this in a low volume way.

*Creating a Message using JSON *

The Microsoft Graph is a REST based API that for the most part uses JSON as its internal format. So if you want to upload a message you need to create a JSON representation of that message that can be used in the Microsoft Graph. By default doing this will create a draft so when you want to create a Message in Exchange that you don't want to appear as a Draft there are a few extended properties that need to be set when you create the Message. The first property that needs to be set is the Message Flags pidtagmessageflags

https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagmessageflags-canonical-property

In EWS (Exchange Web Services) setting this property to 1 and using the MIMEContent property was generally the easiest way of uploading a Message but the MIMEContent is done a little differently in the Graph so this method has no direct equivalent. In the Microsoft Graph you can set the pidtagmessageflags property to either 1 or 4, if you set it to 1 the message will be marked as Read (and generally won't cause any notifications) if you set it to 4 in will be marked Unread and for mobile clients this will generally popup a new mail notification.

setting this property in the Graph looks like

"singleValueExtendedProperties": [
    {
        "id": "Integer 0x0E07",
        "value": "1"
    }
]
Enter fullscreen mode Exit fullscreen mode

If you just want to create a Message and Mark is as sent you can use

{  
    "Subject": "Test123",  
    "From": {  
        "EmailAddress": {  
            "Name": "senderblah",  
            "Address": "senderblah@blah.com"  
        }  
    },  
    "Body": {  
        "ContentType": "HTML",  
        "Content": "Just the facts"  
    },  
    "ToRecipients": [  
        {  
            "EmailAddress": {  
                "Name": "blah",  
                "Address": "blah@blah.com"  
            }  
        }  
    ],  
    "SingleValueExtendedProperties": [  
        {  
            "Id": "Integer 0x0E07",  
            "Value": "1"  
        },  
    ]  
}  
Enter fullscreen mode Exit fullscreen mode

Time Travel

If you wanted the above message to appear as it had been sent in the past (eg if it was from an external system where you need to maintain the datetime it was received). Then you also need to set a couple of other properties when you create the message

Pidtagmessagedeliverytime
https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagmessagedeliverytime-canonical-property

and

PidTagClientSubmitTime
https://learn.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagclientsubmittime-canonical-property

setting these properties will look like

"SingleValueExtendedProperties": [
    {
        "PropertyId": "Integer 0x0E07",
        "Value": "1"
    },
    {
        "PropertyId": "SystemTime 0x0039",
        "Value": "2023-06-12T10:10:47.2048+10:00"
    },
    {
        "PropertyId": "SystemTime 0x0E06",
        "Value": "2023-06-12T10:10:47.2048+10:00"
    }
]
Enter fullscreen mode Exit fullscreen mode

Graph SDK

If you want to use the Graph SDK to create the Message then an example of doing this would look like

        var requestBody = new Message
        {
            Subject = "Did you see last night's game?",
            Importance = Importance.Normal,
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = "They were <b>awesome</b>!",
            },
            ToRecipients = new List<Recipient>
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "user@domain.com",
                    },
                },
            },
            From = new Recipient()
            {
                EmailAddress = new EmailAddress
                {
                    Address = "from@domain.com",
                },
            },
            SingleValueExtendedProperties = new List<SingleValueLegacyExtendedProperty> {
                new SingleValueLegacyExtendedProperty {
                    Id = "Integer 0x0E07",
                    Value = "1"
                },
                new SingleValueLegacyExtendedProperty {
                    Id = "SystemTime 0x0039",
                    Value = "2023-09-29T10:10:47.2048+10:00"
                },
                new SingleValueLegacyExtendedProperty {
                    Id = "SystemTime 0x0E06",
                    Value = "2023-09-29T10:10:47.2048+10:00"
                }
            }
        };
        var result = graphServiceClient.Users["user@domain.com"].MailFolders["Inbox"].Messages.PostAsync(requestBody).GetAwaiter().GetResult();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)