如何从此API代码发送邮件?

时间:2014-07-28 17:00:56

标签: java email mailgun

我正在使用Eclipse LUNA包。我需要使用名为“MAILGUN”的服务提供商发送邮件。在“www.mailgun.com”网站上,他们提供了使用可用服务发送或接收邮件的API代码。代码如下:

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");
    }
}

此处,我的服务提供商密钥将被替换,而不是“key- * ”。我的输出是:

    Success

但是,邮件无法发送。请指导我如何使用mailgun作为服务提供商...

3 个答案:

答案 0 :(得分:1)

您是否尝试过更改" samples.mailgun.org"到你的域名?

另外,您实际上并未发布数据:

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>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.10</version>
    </dependency>

答案 1 :(得分:1)

请尝试此代码。 我正在使用下面的代码,它的工作正常。

    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();

答案 2 :(得分:1)

如果有人发现这个有用,我正在开发一个Java邮件库,以便使用Mailgun轻松发送电子邮件。

https://github.com/sargue/mailgun

它允许发送这样的消息:

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

即使文件附件也很简单:

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();

还支持异步消息发送和HTML邮件助手。这是一个年轻的项目,非常欢迎反馈。