Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java REST Mailgun

Tags:

java

rest

mailgun

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", "[email protected]");
       formData.add("to", "[email protected]");
       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

like image 640
user3586514 Avatar asked Mar 14 '15 21:03

user3586514


People also ask

Is Mailgun an API?

Regardless of your business case, Mailgun Email API empowers senders like you to build with the most powerful and reliable email service provider in the industry.

Is Mailgun API free?

Free Email API For Easy Sending. Send, receive, and track emails with Mailgun's free email API.

Does Mailgun send SMS?

TextMagic, also a Mailgun customer, allows you to send notifications, alerts, reminders, confirmations and SMS marketing campaign messages to your customers, staff members and suppliers. On average, TextMagic customers are sending over 2.5M text messages around the world each month.


2 Answers

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("[email protected]")
    .subject("This is the subject")
    .text("Hello world!")
    .build()
    .send();

Even file attachments are easy:

MailBuilder.using(configuration)
    .to("[email protected]")
    .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.

like image 176
sargue Avatar answered Oct 17 '22 06:10

sargue


You need the following dependencies:

  • jersey-core: http://mvnrepository.com/artifact/com.sun.jersey/jersey-core
  • jersey-client: http://mvnrepository.com/artifact/com.sun.jersey/jersey-client
  • jersey-multipart: http://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart

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>
like image 39
Gillis Van Ginderachter Avatar answered Oct 17 '22 06:10

Gillis Van Ginderachter