0

Previously, I was successfully sending emails with this code.

SendMailPostRequestBody postRequest = new SendMailPostRequestBody();
postRequest.setMessage(message);
postRequest.setSaveToSentItems( this.saveToSentItems );
graphClient
    .users().byUserId( this.username )
    .sendMail()
    .post( postRequest );

But I wanted a reliable message id. The official documentation says

You can use immutable IDs to find a message in the Sent Items folder after it has been sent, using the following steps:

  1. Create a draft message using the Prefer: IdType="ImmutableId" header and save the id property of the message in the response.
  2. Send the message using the ID from the previous step.
  3. Get the message using the ID from the first step. This is the copy in Sent Items.

So now I have this code, which is missing only the Prefer: IdType="ImmutableId" part:

Message posted = graphClient.users().byUserId( this.username )
                            .messages()
                            .post( message );
String postedId = posted.getId();
if( postedId != null ) {
    graphClient.users().byUserId( this.username )
               .messages()
               .byMessageId( postedId )
               .send()
               .post();
    Message gotten = graphClient.users().byUserId( this.username )
                                .messages()
                                .byMessageId( postedId )
                                .get();
    return gotten.getId();
}

This sends correctly, but without the Prefer: IdType="ImmutableId" it blows up on the line Message gotten = .... .get() with

com.microsoft.graph.models.odataerrors.ODataError: The specified object was not found in the store., The process failed to get the correct properties.
    at com.microsoft.graph.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36)
    at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:212)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:704)
    at com.microsoft.kiota.ApiExceptionBuilder.<init>(ApiExceptionBuilder.java:26)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:703)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:307)
    at com.microsoft.graph.users.item.messages.item.MessageItemRequestBuilder.get(MessageItemRequestBuilder.java:194)
    at com.microsoft.graph.users.item.messages.item.MessageItemRequestBuilder.get(MessageItemRequestBuilder.java:181)

Again following official docs on how to set headers, I'm doing this:

ItemBody html = new ItemBody();
html.setContentType( BodyType.Html );
html.setContent( msg.htmlEmail() );
Message message = new Message();
message.setSubject( msg.subjectLine() );
message.setBody( html );

LinkedList<InternetMessageHeader> internetMessageHeaders = new LinkedList<InternetMessageHeader>();
InternetMessageHeader internetMessageHeader = new InternetMessageHeader();
internetMessageHeader.setName("Prefer"); // Also tried with "X"
internetMessageHeader.setValue("IdType=\"ImmutableId\"");
internetMessageHeaders.add(internetMessageHeader);
message.setInternetMessageHeaders(internetMessageHeaders);

return message;

However, this gives the below exception.

com.microsoft.graph.models.odataerrors.ODataError: The internet message header name 'Prefer' should start with 'x-' or 'X-'.
    at com.microsoft.graph.models.odataerrors.ODataError.createFromDiscriminatorValue(ODataError.java:36)
    at com.microsoft.kiota.serialization.JsonParseNode.getObjectValue(JsonParseNode.java:212)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.lambda$throwIfFailedResponse$0(OkHttpRequestAdapter.java:704)
    at com.microsoft.kiota.ApiExceptionBuilder.<init>(ApiExceptionBuilder.java:26)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.throwIfFailedResponse(OkHttpRequestAdapter.java:703)
    at com.microsoft.kiota.http.OkHttpRequestAdapter.send(OkHttpRequestAdapter.java:307)
    at com.microsoft.graph.users.item.messages.MessagesRequestBuilder.post(MessagesRequestBuilder.java:116)
    at com.microsoft.graph.users.item.messages.MessagesRequestBuilder.post(MessagesRequestBuilder.java:101)

If I include the "X", it goes back to being unable to find the email again.

So what am I doing wrong?

2
  • 1
    This is supposed to be an HTTP request header, that you send with your API request, not a header for the email message itself. learn.microsoft.com/en-us/graph/sdks/… Commented Oct 6 at 13:01
  • @C3roe Thank you. That helped me get it working. I would post my working code as an answer, but if you would like to, I would accept so you get the points. I was able to use .post( message, request -> request.headers.put("Prefer", Set.of("IdType=\"ImmutableId\""))); A one-line fix! Commented Oct 6 at 13:43

1 Answer 1

4

Prefer is supposed to be an HTTP request header send with your API request, not an email header you would add to the message itself.

https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=java#use-http-headers-to-control-request-behavior has an example of how to set it with the JAVA SDK:

// GET https://graph.microsoft.com/v1.0/me/events
final EventCollectionResponse events = graphClient.me().events().get( requestConfiguration -> {
    requestConfiguration.headers.add("Prefer", "outlook.timezone=\"Pacific Standard Time\"");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.