Java REST Mailgun

心不动则不痛 提交于 2019-12-21 09:22:17

问题


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


回答1:


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.




回答2:


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>



回答3:


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



来源:https://stackoverflow.com/questions/29054685/java-rest-mailgun

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