Search code examples
c#microsoft-graph-apisendmailmessageid

Retrieve messageId when sending mails with graph api


As per design sending emails seem to only provide a status code and no further information.

If successful, this method returns 202 Accepted response code. It doesn't return anything in the response body.

How am I supposed to get the messageId of my send mail? I can't seem to find that information anywhere online.

Or is there any way to set it before sending?

Or do I need to save a draft and send it with an additional call to graph api resulting in two calls instead of one?

This is what I got so far:

        var options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud };
        var credentials = new UsernamePasswordCredential(configuration.EmailAddress, configuration.Password, 
            configuration.TenantId, configuration.ClientId, options);

        var graphClient = new GraphServiceClient(credentials, new[] { "https://graph.microsoft.com/.default" });

        var body = new Microsoft.Graph.Me.SendMail.SendMailPostRequestBody();

        //configuration of the mail goes here

        await graphClient.Me.SendMail.PostAsync(body);

Also any idea howto get the actual send message? Coming from Mimekit I could easily convert it into a MemoryStream.


Solution

  • Message Send's are asynchronous which means the API won't return the I'd of the Message, saving the Message in the Mailboxes Sent Items is also option which is all part of the Submission process.

    Or do I need to save a draft and send it with an additional call to graph api resulting in two calls instead of one?

    If you use ImmutableId's https://learn.microsoft.com/en-us/graph/outlook-immutable-id and then make sure you save the Item to the SendItems Folder you will be able to access it (export it to mime if that's what you want to do etc).

    How am I supposed to get the messageId of my send mail? I can't seem to find that information anywhere online.

    Or is there any way to set it before sending?

    You can set the InternetMessageId by setting the PidTagInternetMessageId property on send eg

    {
        "message": {
            "subject": "Meet for lunch?",
            "body": {
                "contentType": "Text",
                "content": "The new cafeteria is open."
            },
            "toRecipients": [
                {
                    "emailAddress": {
                        "address": "[email protected]"
                    }
                }
            ],
            "singleValueExtendedProperties": [
                {
                    "id": "String 0x1035",
                    "value": "[email protected]"
                }
            ]
        }
    }

            private static void SendMessageTest(GraphServiceClient graphServiceClient, string emailAddress, string user)
            {
                var requestBody = new SendMailPostRequestBody()
                {
                    Message = 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 = emailAddress,
                                },
                            },
                        },
                        SingleValueExtendedProperties = new List<SingleValueLegacyExtendedProperty> {
                            new SingleValueLegacyExtendedProperty {
                                Id = "String 0x1035",
                                Value = "[email protected]"
                            }
                        }
                    }
                };  
    
                graphServiceClient.Users[user].SendMail.PostAsync(requestBody).GetAwaiter().GetResult();
    
            }