9

I am trying to take advantage of Mailgun's Transactional Email Service via their RESTful API, but I can not make it work. I am able to send emails via SMTP but i prefer to use their API.

Their documentation provides me with the following code:

public static ClientResponse SendSimpleMessage() {
       Client client = Client.create();
       client.addFilter(new HTTPBasicAuthFilter("api",
                       "key-*****"));
       WebResource webResource =
               client.resource("https://api.mailgun.net/v2/DOMAIN" +
                               "/messages");
       MultivaluedMapImpl formData = new MultivaluedMapImpl();
       formData.add("from", "Excited User <mailgun@DOMAIN>");
       formData.add("to", "bar@example.com");
       formData.add("to", "bar@example.com");
       formData.add("subject", "Hello");
       formData.add("text", "Testing some Mailgun awesomness!");
       return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).
               post(ClientResponse.class, formData);
}

Obviously I need some kind of REST client to take advantage of this code, but I have not been able to find anything online that works for me. Can someone please explain to me step by step how I make this work. I am using Eclipse, JAVA EE, No Maven

user3586514
  • 123
  • 1
  • 2
  • 4

3 Answers3

9

I'm developing a Java mail library to easily send email messages using Mailgun. It may fit your needs.

https://github.com/sargue/mailgun

It allows you to send messages like this:

MailBuilder.using(configuration)
    .to("marty@mcfly.com")
    .subject("This is the subject")
    .text("Hello world!")
    .build()
    .send();

Even file attachments are easy:

MailBuilder.using(configuration)
    .to("marty@mcfly.com")
    .subject("This message has an text attachment")
    .text("Please find attached a file.")
    .multipart()
    .attachment(new File("/path/to/image.jpg"))
    .build()
    .send();

There is also support for asynchronous message sending and a HTML mail helper. It is a young project, feedback is very welcome.

sargue
  • 5,695
  • 3
  • 28
  • 43
1

You need the following dependencies:

You can download the JARs from mvnrepository and add them to your classpath.

Use the following dependencies if you should switch to Maven:

<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-core</artifactId>
  <version>1.19</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey</groupId>
  <artifactId>jersey-client</artifactId>
  <version>1.19</version>
</dependency>
<dependency>
  <groupId>com.sun.jersey.contribs</groupId>
  <artifactId>jersey-multipart</artifactId>
  <version>1.19</version>
</dependency>
  • Thank you very much for your response. I don't use Maven and I have no intention of using Maven. Is there a way to implement this without using Maven? – user3586514 Mar 31 '15 at 16:49
  • 1
    You can follow the mvnrepository links. Then click the newest version, i.e. "1.19" and then click "Download (JAR)". Finally, add the JAR to the classpath of your project. – Gillis Van Ginderachter Apr 01 '15 at 17:53
0

Maybe try with this post, using basic implementation of MailGun API with Retrofit library: send mail from android without using smtp and user interaction

Community
  • 1
  • 1
Hpsaturn
  • 2,702
  • 31
  • 38