7

I am using https://github.com/sendgrid/sendgrid-java in my app. This is my code

SendGrid sendgrid = new SendGrid("username", "password");
SendGrid.Email email = new SendGrid.Email();
email.addTo("[email protected]");
email.setFrom("[email protected]");
email.setSubject("Hello World");
email.setText("My first email with SendGrid Java!");

try {
  SendGrid.Response response = sendgrid.send(email);
  System.out.println(response.getMessage());
}
catch (SendGridException e) {
  System.err.println(e);
}

This is my maven code

<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>2.2.0</version>
</dependency>

While running this code I get the following error.

java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:497)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:270)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:498)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:434)

Root cause

java.lang.NoClassDefFoundError: org/apache/http/impl/client/HttpClientBuilder
com.sendgrid.SendGrid.<init>(SendGrid.java:67)

I am new to java, I dont know what this means. Thanks in advance.

5 Answers 5

2

There is a problem in maven dependency for sendgrid. So I just copied the class from the https://github.com/sendgrid/sendgrid-java to my app. There was only two class in it. Also I copied the dependency from the sendgrid-java and added to my app. It worked.

9
  • 2
    Is there still an issue with the maven dependency? Because I'm experiencing the same at the moment (worked before thou)
    – Perneel
    Dec 31, 2015 at 15:21
  • @Perneel Now this is fixed, I tried today, its working fine. Just add the jar file and start sending email
    – Subash
    Jan 6, 2016 at 6:33
  • @Subash what version are you running? I tried it with 2.2.2 and i get the NoClassDefFoundException
    – DeiAndrei
    Jan 6, 2016 at 16:20
  • @DeiAndrei Use 2.2.1 I have tested that, it is working fine
    – Subash
    Jan 7, 2016 at 8:24
  • I'm getting NoClassDefFoundException using 2.2.1 with Gradle. Mar 4, 2016 at 17:21
1

I recommend doing a mvn dependency:tree and scanning your tree for the following class: org.apache.http.impl.client.HttpClientBuilder

I am migrating from Mandrill to SendGrid and the reason is that my mandril dependency was 'higher' in the list than my sendgrid-java dependency, and Mandrill used a version 4.2 of the httpclient artifact.

My fix was to add an exclusion to remove that artifact from Mandrill

    <dependency>
        <groupId>com.mandrillapp.wrapper.lutung</groupId>
        <artifactId>lutung</artifactId>
        <version>0.0.4</version>
        <exclusions>
            <exclusion>
                <artifactId>httpclient</artifactId>
                <groupId>org.apache.httpcomponents</groupId>
            </exclusion>
        </exclusions>
    </dependency>

Obviously for you it might be different, but I'm willing to bet you're dependency is bringing in an older version f the http client.

0
0
    <dependency>
        <groupId>com.sendgrid</groupId>
        <artifactId>sendgrid-java</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>com.sendgrid</groupId>
        <artifactId>java-http-client</artifactId>
        <version>4.1.0</version>
    </dependency>
1
  • This adds missing dependencies to your code. The error you got was because your code missed dependencies that are used in yet another dependency. If you still have a problem understanding your error I suggest you read up on dependency management in Java.
    – Michael
    Aug 10, 2017 at 16:20
0

I have solved. I have moved the maven dependency from a service/pom.xml to the parent module pom.xml

mvn dependency:tree to see the dependency in each module

only using the followinng dependency

<dependency>
    <groupId>com.sendgrid</groupId>
    <artifactId>sendgrid-java</artifactId>
    <version>4.0.1</version>
</dependency>
0
    public async Task<bool> CreateMailItem(string email, string name, string subject, string body)
    {
        string SendGridEmail = _configuration.GetSection("EmailSendGrid:Email").Value;
        string APIKey = _configuration.GetSection("EmailSendGrid:Key").Value;
        
        SendGridClient client = new SendGridClient(APIKey);
        EmailAddress from = new EmailAddress(SendGridEmail, "Teste de Envio");            
        EmailAddress to = new EmailAddress(email, name);
        SendGridMessage msg = MailHelper.CreateSingleEmail(from, to, subject, "", body);
        var response = await client.SendEmailAsync(msg);

        return response.IsSuccessStatusCode;
    }
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 13, 2022 at 6:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.