0

I'm trying to get the libraries needed to send mail on AppEngine.

The docs (https://cloud.google.com/appengine/docs/standard/java/mail/mailgun) state that these need to be added if using Maven:

jersey-core 1.19.4

jersey-client 1.19.4

jersey-multipart 1.19.4

Adding them however, results in an error message in Eclipse.

The type javax.ws.rs.ext.RuntimeDelegate$HeaderDelegate cannot be resolved. It is indirectly referenced from required .class files

That can be resolved by adding javax.ws.rs-api-2.0.1, but using that jar seems problematic.

Appengine is throwing an error:

Caused by: java.lang.ExceptionInInitializerError

    ... 44 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl

    at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
    ... 62 more
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.internal.RuntimeDelegateImpl
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at com.google.apphosting.runtime.ApplicationClassLoader.findClass(ApplicationClassLoader.java:45)

In a thread here though that error org.glassfish.jersey.internal.RuntimeDelegateImpl NOT FOUND is said to be solvable by removing javax.ws.rs-api-2.0

Other suggestions include using jersey2, but that requires additional configuration I believe and the AppEngine docs don't show how to do that.

How can I send mail on Appengine using Mailgun???

user1258245
  • 3,639
  • 2
  • 18
  • 23
  • You'll have to provide more details about what you're trying to do as using the sample code works for me. Have you tried using the sample project at https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/appengine-java8/mailgun? Are you trying to use a non-Maven project? – Brian de Alwis Mar 26 '18 at 16:45
  • Yes, it was non-Maven. – user1258245 Jun 07 '18 at 09:14

1 Answers1

0

I solved it by using Jersey 2.27 (JAX-RS 2.1 / Jersey 2.26+) available as a download from https://jersey.github.io/download.html

I needed the following jars:

        hk2-api-2.5.0-b32.jar
        hk2-locator-2.5.0-b42.jar
        hk2-utils-2.5.0-b32.jar
        javax.inject-1.jar
        javax.inject-2.5.0-b42.jar
        jersey-client.jar
        jersey-common.jar
        jersey-guava-2.26-b03.jar (from Jersey 2.25.1)
        jersey-media-jaxb.jar
        javax.ws.rs-api-2.1.jar

And modified the AppEngine sample to use Jersey 2.26+ as below:

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

public static void sendSimpleMessage(String recipient) {
        Client clientMail = ClientBuilder.newBuilder().build();
              clientMail.register(HttpAuthenticationFeature.basic("api", MAILGUN_API_KEY));

        WebTarget targetMail = clientMail.target("https://api.mailgun.net/v3/" + MAILGUN_DOMAIN_NAME + "/messages");

        Form formData = new Form();
              formData.param("from", "Mailgun Sandbox <"+DOMAIN_MAIL_ADDRESS+">");
              formData.param("to", recipient);
              formData.param("subject", "Simple Mailgun Example");
              formData.param("text", "Plaintext content");

        Response response = targetMail.request().post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
        System.out.println("Mail sent : " + response);

    }

The MAILGUN_API_KEY is available from MailGun. MAILGUN_DOMAIN_NAME is my custom domain and DOMAIN_MAIL_ADDRESS is the address I wish to send from.

Note: I have guava-gwt and gauva on my classpath too, so perhaps it was the addition of jersey-guava that did the trick. Actually, jersey-gauva was only in jaxrs-ri-2.25.1 and not in jaxrs-ri-2.27 (JAX-RS 2.1 / Jersey 2.26+). It was definitely needed though.

user1258245
  • 3,639
  • 2
  • 18
  • 23