SendGrid - NoClassDeffoundError using sendgrid-jav

2019-09-20 10:33发布

问题:

I am using SendGrid for the first time. I have looked at other questions on SO, in this regard, but none worked for me.

I am using Eclipse and Amazon Web Services - Lambda. The library i am using is https://github.com/sendgrid/sendgrid-java

My scenario is, i am using a Lambda function to send emails to users who have opted for email communication.

Here is my code:

                        Email email = new Email();
                        email.addTo(DEV_TO_EMAIL);
                        email.addToName("XXXXX");
                        email.setFrom(DEV_FROM_EMAIL);
                        email.setSubject("XX");
                        email.setText("sms_message");

                        try {
                            sendgrid.send(email);
                        } catch (SendGridException e) {
                            e.printStackTrace();

                            if(DEBUG)
                                logger.log("Exception while sending Email");
                        }

Here is the Exception:

    Could not initialize class 

org.apache.http.impl.conn.ManagedHttpClientConnectionFactory: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: Could not initialize class org.apache.http.impl.conn.ManagedHttpClientConnectionFactory at 

org.apache.http.impl.conn.PoolingHttpClientConnectionManager$InternalConnectionFactory.<init>(PoolingHttpClientConnectionManager.java:493) at 

org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:149) at 

org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:138) at 

org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:114) at 

org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:726) at 

com.sendgrid.SendGrid.<init>(SendGrid.java:71) at 

xx.LambdaFunction.handleRequest(LambdaFunction.java:183) at 

xx.LambdaFunction.handleRequest(LambdaFunction.java:1)

I tried to follow this suggestion on SO, Sendgrid Java Not working in Maven, but it resulted in the same error as well.

Any suggestions on what to do? Let me know if you need anything else..

回答1:

I tried most of the solutions posted on SO, but couldn't get it to work. Finally, i switched to using SendGrid4j. This worked after adding httpclient, httpcore, httpmime along with gson, jackson-core, libraries.

link for sendgid4j - https://github.com/shunjikonishi/sendgrid4j