SendGrid - NoClassDeffoundError using sendgrid-java.jar

半城伤御伤魂 提交于 2019-12-14 03:28:44

问题


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



来源:https://stackoverflow.com/questions/37266668/sendgrid-noclassdeffounderror-using-sendgrid-java-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!