msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

Email with attachment causes Exception: NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement;

Open goksasa opened this issue 3 years ago • 3 comments

Expected behavior

Send Email with attachment.

Actual behavior

Exception: NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; Caused by: java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement;
at com.microsoft.graph.serializer.CollectionPageSerializer.serialize(CollectionPageSerializer.java:78)

Looks like there is any conflict with Gson libraries. Sending emails works fine without attachments but crashes with them.

Steps to reproduce the behavior

Import graph sdk through Maven dependencies: <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph</artifactId> 5.35.0

Send email with file attachment, exactly like the official doc example says how to do

goksasa avatar Sep 22 '22 13:09 goksasa

Hello,

Thanks in advance for the help.

If it may help you I'm attaching the detailed issue I also opened in SO .

Thanks for your help.

goksasa avatar Sep 22 '22 15:09 goksasa

Hi @goksasa , I noticed that you are using a class called Mail as a parameter type in your createMessage method. Could you expand a bit on the properties of that class. In attempting to replicate your scenario I noticed that the snippet you were using to model your request was missing an important line: attachment.oDataType = "#microsoft.graph.fileAttachment"; After I added this line I had no issues replicating the code snippet and didn't run into any errors sending an email with an attachment. Further, could you share your dependency list? Here is the code I am using to send an email, I can confirm this works on my end.

        Message message = new Message();
        ItemBody body = new ItemBody();
        body.contentType = BodyType.HTML;
        body.content = "HELLO!";
        message.body = body;
        LinkedList<Recipient> recipients = new LinkedList<Recipient>();
        Recipient recipient = new Recipient();
        EmailAddress email = new EmailAddress();
        email.address = "[email protected]";
        recipient.emailAddress = email;
        recipients.add(recipient);
        message.toRecipients = recipients;
        LinkedList<Attachment> attachmentList = new LinkedList<>();
        FileAttachment attachment = new FileAttachment();
        attachment.name = "attachment.txt";
        attachment.contentType = "text/plain";
        attachment.oDataType = "#microsoft.graph.fileAttachment";
        attachment.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
        attachmentList.add(attachment);
        AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
        attachmentCollectionResponse.value = attachmentList;
        AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(attachmentCollectionResponse, null);
        message.attachments = attachmentCollectionPage;
        graphClient.me().sendMail(
                        UserSendMailParameterSet.newBuilder().withMessage(message).withSaveToSentItems(null).build()
                ).buildRequest().post();
//Dependencies I used are:
    implementation 'com.microsoft.graph:microsoft-graph:5.36.0'
    implementation 'com.azure:azure-identity:1.4.0'

ramsessanchez avatar Sep 29 '22 00:09 ramsessanchez

Hi @ramsessanchez ,

First of all thank you for the support :)

I copied exactly this same code and changed my dependencies versions to match yours. So this is my actual status:

Dependencies

    <dependencies>
        <dependency>
            <groupId>com.microsoft.azure.functions</groupId>
            <artifactId>azure-functions-java-library</artifactId>
            <version>${azure.functions.java.library.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.microsoft.graph/microsoft-graph -->
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>5.36.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.azure/azure-identity -->
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.4.0</version>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure-storage -->
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-storage</artifactId>
            <version>8.6.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.13.4</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>

        <!-- FOR SOAP CALLS -->
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>rt</artifactId>
            <version>2.3.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.6.2</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

    </dependencies>

The send email with attachment with graph api

    public void sendEmailWithAttachment(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(createMessageWithAttachment(context)).build())
                    .buildRequest()
                    .post();
        } catch (ClientException | IllegalArgumentException | UnsupportedEncodingException e) {
            context.getLogger().warning("Couldn't send the email due to errors while reading the input message");
            context.getLogger().warning(e.getMessage());
        }
        
    }

The replicated Message object creation

private Message createMessageWithAttachment(ExecutionContext context) throws UnsupportedEncodingException, IllegalArgumentException{
        Message message = new Message();
        ItemBody body = new ItemBody();
        body.contentType = BodyType.HTML;
        body.content = "HELLO!";
        message.body = body;
        LinkedList<Recipient> recipients = new LinkedList<Recipient>();
        Recipient recipient = new Recipient();
        EmailAddress email = new EmailAddress();
        email.address = "[email protected]";
        recipient.emailAddress = email;
        recipients.add(recipient);
        message.toRecipients = recipients;
        LinkedList<Attachment> attachmentList = new LinkedList<>();
        FileAttachment attachment = new FileAttachment();
        attachment.name = "attachment.txt";
        attachment.contentType = "text/plain";
        attachment.oDataType = "#microsoft.graph.fileAttachment";
        attachment.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
        attachmentList.add(attachment);
        AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
        attachmentCollectionResponse.value = attachmentList;
        AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(attachmentCollectionResponse, null);
        message.attachments = attachmentCollectionPage;
        return message;
    }

The error

[2022-09-29T08:25:23.797Z] Message processing error (Action=UserCallback, ClientId=MessageReceiver1mailslocal, EntityPath=mailslocal, Endpoint=quirumed.servicebus.windows.net)
[2022-09-29T08:25:23.797Z] 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-29T08:25:23.798Z]      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2022-09-29T08:25:23.799Z]      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[2022-09-29T08:25:23.799Z]      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[2022-09-29T08:25:23.800Z]      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[2022-09-29T08:25:23.801Z]      at com.microsoft.azure.functions.worker.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
[2022-09-29T08:25:23.802Z]      at com.microsoft.azure.functions.worker.broker.EnhancedJavaMethodExecutorImpl.execute(EnhancedJavaMethodExecutorImpl.java:55)
[2022-09-29T08:25:23.802Z]      at com.microsoft.azure.functions.worker.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:54)
[2022-09-29T08:25:23.803Z]      at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
[2022-09-29T08:25:23.803Z]      at com.microsoft.azure.functions.worker.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
[2022-09-29T08:25:23.804Z]      at com.microsoft.azure.functions.worker.handler.MessageHandler.handle(MessageHandler.java:45)
[2022-09-29T08:25:23.805Z]      at com.microsoft.azure.functions.worker.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:92)
[2022-09-29T08:25:23.806Z]      at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
[2022-09-29T08:25:23.807Z]      at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[2022-09-29T08:25:23.808Z]      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
[2022-09-29T08:25:23.808Z]      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
[2022-09-29T08:25:23.809Z]      at java.base/java.lang.Thread.run(Thread.java:834)
[2022-09-29T08:25:23.809Z] Caused by: java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement;
[2022-09-29T08:25:23.810Z]      at com.microsoft.graph.serializer.CollectionPageSerializer.serialize(CollectionPageSerializer.java:78)
[2022-09-29T08:25:23.810Z]      at com.microsoft.graph.serializer.GsonFactory.lambda$getGsonInstance$10(GsonFactory.java:188)
[2022-09-29T08:25:23.811Z]      at com.google.gson.internal.bind.TreeTypeAdapter.write(TreeTypeAdapter.java:81)
[2022-09-29T08:25:23.811Z]      at com.google.gson.internal.bind.TreeTypeAdapter.write(TreeTypeAdapter.java:74)
[2022-09-29T08:25:23.812Z]      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
[2022-09-29T08:25:23.813Z]      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
[2022-09-29T08:25:23.813Z]      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
[2022-09-29T08:25:23.814Z]      at com.microsoft.graph.serializer.ODataTypeParametrizedIJsonBackedTypedAdapter.write(ODataTypeParametrizedIJsonBackedTypedAdapter.java:45)       
[2022-09-29T08:25:23.815Z]      at com.microsoft.graph.serializer.ODataTypeParametrizedIJsonBackedTypedAdapter.write(ODataTypeParametrizedIJsonBackedTypedAdapter.java:22)       
[2022-09-29T08:25:23.817Z]      at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
[2022-09-29T08:25:23.818Z]      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
[2022-09-29T08:25:23.818Z]      at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
[2022-09-29T08:25:23.819Z]      at com.google.gson.Gson.toJson(Gson.java:704)
[2022-09-29T08:25:23.820Z]      at com.google.gson.Gson.toJsonTree(Gson.java:597)
[2022-09-29T08:25:23.820Z]      at com.google.gson.Gson.toJsonTree(Gson.java:576)
[2022-09-29T08:25:23.821Z]      at com.microsoft.graph.serializer.DefaultSerializer.serializeObject(DefaultSerializer.java:230)
[2022-09-29T08:25:23.822Z]      at com.microsoft.graph.http.CoreHttpProvider.getHttpRequest(CoreHttpProvider.java:308)
[2022-09-29T08:25:23.822Z]      at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:405)
[2022-09-29T08:25:23.824Z]      at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:225)
[2022-09-29T08:25:23.825Z]      at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:202)
[2022-09-29T08:25:23.826Z]      at com.microsoft.graph.http.BaseRequest.send(BaseRequest.java:335)
[2022-09-29T08:25:23.826Z]      at com.microsoft.graph.requests.UserSendMailRequest.post(UserSendMailRequest.java:54)
[2022-09-29T08:25:23.827Z]      at com.quirumed.queuemail.utils.MicrosoftGraphApiUtils.sendEmailWithAttachment(MicrosoftGraphApiUtils.java:80)
[2022-09-29T08:25:23.827Z]      at com.quirumed.queuemail.QueueTriggerMails.queueTriggerMails(QueueTriggerMails.java:62)

goksasa avatar Sep 29 '22 08:09 goksasa

Sorry for the late reply, we've been working hard to release a new major version of the SDK Since this is an issue with the older version, we'll be closing it for now. Please go ahead and try with this new version, if you're still facing issues, feel free to open a new issue.

baywet avatar Feb 08 '24 13:02 baywet