401 on graph.microsoft.com/v1.0/users/.../microsoft.graph.sendMail

Kiril Arsov 20 Reputation points
2025-05-14T07:45:19.0533333+00:00

I am using this library


<dependency>
    <groupId>com.microsoft.graph</groupId>
    <artifactId>microsoft-graph</artifactId>
    <version>5.74.0</version>
</dependency>

in a spring boot app. I have successfully setup the client like this


@Bean
public GraphServiceClient graphServiceClient() {
    try {
        final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId("...")
                .clientSecret("...")
                .tenantId("...")
                .build();
        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(
                List.of("https://graph.microsoft.com/.default"),
                clientSecretCredential
        );
        return GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

and when I try to list users using

graphServiceClient.users()
UserCollectionPage usersPage = graphServiceClient.users()
        .buildRequest()
        .select("displayName,userPrincipalName,mail")
        .get();
graphServiceClient.users()

everything works fine, so the access token and my graph client is configured fine.

When I try to send an email by using

graphServiceClient.users("...")
        .sendMail(UserSendMailParameterSet
                .newBuilder()
                .withMessage(message)
                .withSaveToSentItems(true)
                .build())
        .buildRequest()
        .post();

I get this error that I can not recover from

com.microsoft.graph.http.GraphServiceException: Error code: Unable to parse error response message
Error message: Raw error: 
POST https://graph.microsoft.com/v1.0/users/.../microsoft.graph.sendMail
SdkVersion : graph-java/v5.74.0
[...]
401 : 
[...]

I have properly created the application and have added the required permissions.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
43,569 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Kiril Arsov 20 Reputation points
    2025-05-16T10:34:59.4866667+00:00
    0 comments No comments

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.