Microsoft Graph API - NoSuchMethodError com.google.gson.JsonParser.parseString while sending email with attachment
Hello,
I'm posting this issue regarding an error I'm facing while trying to send an email with Microsoft Graph API. ¿Could you please help me to figure out what I'm doing wrong?
I based this development on Microsoft official documentation for sending emails with attachment through Microsoft Graph API:
This is the code where error produces com.microsoft.graph.serializer.CollectionPageSerializer
/**
* Serializes an CollectionPage
*
* @param src the CollectionPage variable for serialization
* @param logger the logger
* @param <T1> the entity type for the collection
* @param <T2> the collection request builder interface type
* @return JsonElement of CollectionPage
*/
@Nullable
public static <T1, T2 extends BaseRequestBuilder<T1>> JsonElement serialize(@Nonnull final BaseCollectionPage<T1, T2> src, @Nonnull final ILogger logger) {
if(src == null) {
return null;
}
Objects.requireNonNull(logger, "parameter logger cannot be null");
JsonArray jsonArray = new JsonArray();
List<T1> items = src.getCurrentPage();
serializer = new DefaultSerializer(logger);
for(T1 item : items) {
final String json = serializer.serializeObject(item);
final JsonElement element = JsonParser.parseString(json);
if(element != null && element.isJsonObject()) {
final JsonObject jsonObject = element.getAsJsonObject();
jsonArray.add(jsonObject);
}
}
return jsonArray;
}
The error
[2022-09-22T09:41:04.345Z] System.Private.CoreLib: Exception while executing function: Functions.QueueTriggerMails. System.Private.CoreLib: Result: Failure Exception: NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; Stack: java.lang.reflect.InvocationTargetException . . . [2022-09-22T09:41:04.356Z] Caused by: java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; [2022-09-22T09:41:04.356Z] at com.microsoft.graph.serializer.CollectionPageSerializer.serialize(CollectionPageSerializer.java:78) . . .
This error only takes place if I add an com.microsoft.graph.models.Attachment list to com.microsoft.graph.models.Message.
Send Email method
public void sendEmail(Mail mailProperties, ExecutionContext context) {
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, clientSecretCredential);
GraphServiceClient<Request> graphClient = GraphServiceClient
.builder()
.authenticationProvider(tokenCredentialAuthProvider)
.buildClient();
try {
graphClient.users(noReplyUserId)
.sendMail(UserSendMailParameterSet
.newBuilder()
.withMessage(createMessage(mailProperties, context))
.withSaveToSentItems(null)
.build())
.buildRequest()
.post();
} catch (ClientException | IllegalArgumentException e) {
context.getLogger().warning("Couldn't send the email due to errors while reading the input message");
context.getLogger().warning(e.getMessage());
}
}
Create Message with Attachment method
private Message createMessage(Mail mailProperties, ExecutionContext context) throws IllegalArgumentException {
Message message = new Message();
ItemBody body = new ItemBody();
body.contentType = BodyType.HTML;
String bodyContent = new String(Base64.getDecoder().decode(mailProperties.getBody()), StandardCharsets.UTF_8);
body.content = bodyContent;
List<Recipient> recipientsList = new ArrayList<>();
for (String item : mailProperties.getDestinatario().split(",")) {
EmailAddress emailAddress = new EmailAddress();
emailAddress.address = item.trim();
Recipient recipient = new Recipient();
recipient.emailAddress = emailAddress;
recipientsList.add(recipient);
}
message.subject = mailProperties.getSubject();
message.body = body;
message.toRecipients = recipientsList;
FileAttachment attachment = new FileAttachment();
attachment.name = "attachment.txt";
attachment.contentType = "text/plain";
attachment.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
List<Attachment> attachmentsList = new ArrayList<Attachment>();
attachmentsList.add(attachment);
AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
attachmentCollectionResponse.value = attachmentsList;
AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(attachmentCollectionResponse, null);
message.attachments = attachmentCollectionPage;
return message;
}
Also, I'm using version 5.31.0 of Graph API:
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>5.31.0</version>
</dependency>