Created
January 3, 2015 12:06
-
-
Save chhabraromit/0c0a16545dd31f471332 to your computer and use it in GitHub Desktop.
Mailgun Example with Jersey 2.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*I have tried this with Jersey 2.14 only but should work for Jersey 2.x*/ | |
package com.mail; | |
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.ClientConfig; | |
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; | |
/** | |
* | |
* @author [email protected] | |
*/ | |
public class SendMail { | |
public static void main(String[] args) { | |
ClientConfig clientConfigMail = new ClientConfig(); | |
Client clientMail = ClientBuilder.newClient(clientConfigMail); | |
clientMail.register(HttpAuthenticationFeature.basic("api",<your-api-key>)); | |
WebTarget targetMail = clientMail.target("https://api.mailgun.net/v2/"+<your-mailgun-subdomain>+"/messages"); | |
Form formData = new Form(); | |
formData.param("from", "Mailgun Sandbox <postmaster@"+<your-mailgun-subdomain>+">"); | |
formData.param("to", <to-email-address>); | |
formData.param("subject", <subject>); | |
formData.param("text", <text>); | |
Response response = targetMail.request().post(Entity.entity(formData,MediaType.APPLICATION_FORM_URLENCODED_TYPE)); | |
//If everything goes correct you should be able to see status 200 in response | |
System.out.println("Mail sent : " + response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 Thanks