How to set both List-Unsubscribe-Post and List-Unsubscribe headers for Microsoft Graph API?

GZ 0 Reputation points
2024-12-09T22:36:02.2466667+00:00

Hi,

I am using the Microsoft graph API to send emails. I need to be able to set two headers List-Unsubscribe-Post and List-Unsubscribe to allow receivers to automatically unsubscribe. I saw https://learn.microsoft.com/en-us/answers/questions/1509500/how-to-provide-list-unsubscribe-header-using-micro already but I need to put two headers and I do not understand where the 'id: 'String 0x1045' in the solution comes from.

Any help would be very much appreciated. My code is:

SendMailPostRequestBody sendMailPostRequestBody = new com.microsoft.graph.users.item.sendmail.SendMailPostRequestBody();

com.microsoft.graph.models.Message message = new Message();
message.setSubject(emailDetails.getSubject());
ItemBody body = new ItemBody();
body.setContentType(BodyType.Html);
body.setContent(emailDetails.getHtmlBody());
message.setBody(body);


LinkedList<Recipient> toRecipients = new LinkedList<>();
Recipient recipient = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress(emailDetails.getTo());
recipient.setEmailAddress(emailAddress);
toRecipients.add(recipient);
message.setToRecipients(toRecipients);
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pardha Saradhi Reddy Jaladi-MSFT 475 Reputation points Microsoft External Staff
    2025-03-20T23:25:34.0666667+00:00

    Hello GZ,

    Thank you for reaching out to Microsoft Support!!

    The id: 'String 0x1045' mentioned in the example you provided corresponds to a custom header that is specified in the MIME format of the email. It is not a specific value that you need to derive or generate, it simply represents an arbitrary identifier for the custom email header being added. These kinds of header identifiers are typically used when constructing MIME email data via APIs such as Microsoft Graph. It isn't directly relevant for Graph API calls.

    Based on what you’ve provided, you are correctly setting up the email subject, body, and recipients using the Microsoft Graph Java SDK. To include the List-Unsubscribe and List-Unsubscribe-Post headers, you'll need to utilize the internetMessageHeaders property of the Message object.

    This Microsoft Graph API documentation, confirms that the internetMessageHeaders property can be used to add custom headers when creating a message. While it doesn't explicitly mention List-Unsubscribe or List-Unsubscribe-Post, it does state that custom headers can be added to a message during its creation. This aligns with the use case of including these headers for unsubscribe functionality. User's image
    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.