How to send mail from this API code?
Pregunta
I am using Eclipse LUNA package. I need to send mail using service provider called "MAILGUN". In that "www.mailgun.com" website, they have given a API code to send or receive mails using the available service. The code is as follows:
import java.awt.PageAttributes.MediaType;
import java.io.*;
import java.net.*;
import javax.annotation.PostConstruct;
import javax.ws.rs.POST;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.core.util.MultivaluedMapImpl;
public class Mil_connect1 {
public static ClientResponse SendSimpleMessage() {
Client client = Client.create();
client.addFilter(new HTTPBasicAuthFilter("api","key-***********"));
com.sun.jersey.api.client.WebResource webResource=client.resource("https://api.mailgun.net/v2/samples.mailgun.org" +"/messages");
MultivaluedMapImpl formData = new MultivaluedMapImpl();
formData.add("from", "skalyanasundaram1994@gmail.com");
formData.add("to", "bharani829@gmail.com");
formData.add("subject", "Hello");
formData.add("text", "Testing some Mailgun awesomness!");
return null;
}
public static void main(String[] args) {
SendSimpleMessage();
System.out.println("Success");
}
}
Here, Instead of "key-*" my service provider secret key will be replaced. My output was:
Success
But, mail cannot be sent. Please kindly guide me how to do that using mailgun as service provider...
Solución
Have you tried changing "samples.mailgun.org" to your domain name?
Also, you are not actually posting your data:
private final String baseURL = "https://api.mailgun.net/v2/";
private String mailgunAPIKey;
private <T> WebTarget createPrivateClient() {
final Client client = ClientBuilder.newClient();
client.register(HttpAuthenticationFeature.basic("api", this.mailgunAPIKey));
return client.target(this.baseURL);
}
protected void fireMailGun(final MultivaluedMap<String, String> postData) {
this.createPrivateClient().path("YOUR_DOMAIN/messages")
.request()
.post(Entity.form(postData));
}
Maven Dependency:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.10</version>
</dependency>
Otros consejos
Please try this code. I am using below code and its working fine.
Properties props = System.getProperties();
props.put("mail.smtps.host","smtp.mailgun.org");
props.put("mail.smtps.auth","true");
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("abc@gmail.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xyz@gmail.com", false));
msg.setSubject(subject);
msg.setText(body);
msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse("abc@gmail.com"));
msg.setContent(body, "text/html");
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");
t.connect("smtp.mailgun.com", "postmaster@sandbox***********.mailgun.org", "0ae971*********");
t.sendMessage(msg, msg.getAllRecipients());
System.out.println("Response: " + t.getLastServerResponse());
t.close();
Just in case someone finds this useful, I'm developing a Java mail library to easily send email messages using Mailgun.
https://github.com/sargue/mailgun
It will allow 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.