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:
- Create a draft message using the
Prefer: IdType="ImmutableId"header and save the id property of the message in the response.- Send the message using the ID from the previous step.
- 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?
.post( message, request -> request.headers.put("Prefer", Set.of("IdType=\"ImmutableId\"")));A one-line fix!