Sending Emails using MS-Graph with list-unsubscribe headers

Anmol Dhingra 20 Reputation points
2023-12-11T11:00:48.36+00:00

Hi Team,

I am facing some issue where i am not able to add list unsubscribe headers without X- prefix using Microsoft graph API.
I have a use case where i want to add List-Unsubscribe and List-Unsubscribe-Post headers but when i try to add it to internetMessageHeaders it only allows headers with X- prefixes.
But for providers to automatically gets the List-Unsubscribe headers to show the Unsusbcribe button the key needs to be exact.

Do we have a way to add these headers to emails sent through Microsoft graph api?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,649 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Macháček Martin 181 Reputation points
    2024-01-11T12:20:32.7666667+00:00

    To include List-Unsubscribe mail header, you need to add it via SingleValueExtendedProperties

    using Microsoft.Graph.Me.SendMail;
    using Microsoft.Graph.Models;
    
    var requestBody = new SendMailPostRequestBody
    {
    	Message = new Message
    	{
    		Subject = "Meet for lunch?",
    		Body = new ItemBody
    		{
    			ContentType = BodyType.Text,
    			Content = "The new cafeteria is open.",
    		},
    		ToRecipients = new List<Recipient>
    		{
    			new Recipient
    			{
    				EmailAddress = new EmailAddress
    				{
    					Address = "johndoe@contoso.com",
    				},
    			},
    		},
    		SingleValueExtendedProperties = new List<SingleValueLegacyExtendedProperty>
    		{
    			new SingleValueLegacyExtendedProperty
    			{
    				Id = "String 0x1045",
    				Value = "<mailto:unsubscribe@reply.contoso.com>, <https://contoso.com/notifications/unsubscribe/123>",
    			},
    		},
    	},
    };
    

    If you need to include also List-Unsubscribe-Post, the solution is more complicated, because there is no way to add List-Unsubscribe-Post via SingleValueExtendedProperties. You need to send message in MIME format.

    Great blog post how to create a MIME message for Graph API is here

    2 people found this answer helpful.