Shawn Chowdhury
Shawn Chowdhury

Reputation: 29

"PKIX path building failed" error when sending email via Microsoft Graph in Spring Boot

I'm working on a Spring Boot application that sends emails using Microsoft Graph. However, when attempting to send an email, I'm encountering SSL handshake errors: "PKIX path building failed" and "unable to find valid certification path to requested target".

Here is the code snippet I'm using to send an email:

public void sendMail() { final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder() .clientId(clientId) .clientSecret(clientSecret) .tenantId(tenantId) .build();

List<String> scopes = new ArrayList<>();
scopes.add("https://graph.microsoft.com/.default");

final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);

final GraphServiceClient graphClient =
        GraphServiceClient
                .builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();

Message message = new Message();
message.subject = "Sample Subject";
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
body.content = getReportHtml(); // Assume getReportHtml() returns a valid HTML string
message.body = body;

LinkedList<Recipient> toRecipientsList = new LinkedList<>();
Recipient toRecipients = new Recipient();
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = "[email protected]";
toRecipients.emailAddress = emailAddress;
toRecipientsList.add(toRecipients);
message.toRecipients = toRecipientsList;

LinkedList<Recipient> ccRecipientsList = new LinkedList<>();
Recipient ccRecipients = new Recipient();
EmailAddress emailAddress1 = new EmailAddress();
emailAddress1.address = "[email protected]";
ccRecipients.emailAddress = emailAddress1;
ccRecipientsList.add(ccRecipients);
message.ccRecipients = ccRecipientsList;

graphClient.users(fromEmailId)
        .sendMail(UserSendMailParameterSet
                .newBuilder()
                .withMessage(message)
                .withSaveToSentItems(true)
                .build())
        .buildRequest()
        .post();

}

Upvotes: 0

Views: 88

Answers (0)

Related Questions