package com.product;
|
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.concurrent.CountDownLatch;
|
import javax.ws.rs.core.MediaType;
|
import com.alibaba.fastjson.JSONObject;
|
import com.mashape.unirest.http.HttpResponse;
|
import com.mashape.unirest.http.JsonNode;
|
import com.mashape.unirest.http.Unirest;
|
import com.mashape.unirest.http.exceptions.UnirestException;
|
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 MailgunSendEmail {
|
|
//////测试账号
|
//private static final String API_KEY = "84d2c563e45e4aa32000a0f608a20e32-cb3791c4-580c662f";
|
//private static final String DOMAIN = "sandbox425b6a32b61840fa902c2e614f4616fc.mailgun.org";
|
|
//////正式账号
|
private static final String API_KEY = "8d52b0a748b20c61331587fa9a3fe6fc-cb3791c4-8cf158ef";
|
private static final String DOMAIN = "mail.purtato.com";
|
private static final String API_URI = "https://api.mailgun.net/v3/"+DOMAIN+"/messages";
|
|
/**
|
* 普通邮件发送,不包含模板
|
* @return
|
* @throws UnirestException
|
*/
|
public JsonNode sendSimpleMessage() throws UnirestException {
|
HttpResponse<JsonNode> request = Unirest.post(API_URI)
|
.basicAuth("api", API_KEY)
|
//测试账号发件人
|
//.queryString("from", "fuller@sandbox425b6a32b61840fa902c2e614f4616fc.mailgun.org")
|
//正式账号发件人
|
.queryString("from", "brianho@mail.purtato.com")
|
//.queryString("to", "b.hokaming@gmail.com")
|
//.queryString("to", "FullerFB@163.com")
|
//.queryString("to","370036259@qq.com")
|
//.queryString("to","brianho1023@yahoo.com")
|
.queryString("subject", "Mailgun test subject")
|
.queryString("html","<html>Testing some Mailgun awesomness! 测试测试测试<p/><i>Bruce</i></html>")
|
//.queryString("text", "Mailgun Testing")
|
.asJson();
|
return request.getBody();
|
}
|
|
/**
|
* 包含模板的邮件发送,首先在mailgun管理端进行模板配置
|
* @return
|
*/
|
public static ClientResponse SendSimpleMessage() {
|
Client client = Client.create();
|
client.addFilter(new HTTPBasicAuthFilter("api", API_KEY));
|
WebResource webResource = client.resource("https://api.mailgun.net/v3/mail.purtato.com/messages");
|
MultivaluedMapImpl formData = new MultivaluedMapImpl();
|
formData.add("from", "brianho@mail.purtato.com");
|
formData.add("subject", "Hello Brian Ho");
|
formData.add("template", "test");
|
formData.add("h:X-Mailgun-Variables", buildTemplateParameter());
|
return webResource.type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, formData);
|
}
|
/**
|
* 组装mailgun模板名称为test的,name、reset_password_url 这两个参数进行传递数据
|
* @return
|
*/
|
public static String buildTemplateParameter() {
|
Map<String, Object> map = new HashMap<String,Object>();
|
map.put("name", "Fuller");
|
map.put("reset_password_url", "https://www.baidu.com");
|
return JSONObject.toJSONString(map);
|
}
|
|
|
|
@SuppressWarnings("static-access")
|
public static void main(String[] args) {
|
|
MailgunSendEmail latchTest = new MailgunSendEmail();
|
|
//////普通测试发送
|
/*
|
JsonNode jsonNode;
|
try {
|
jsonNode = latchTest.sendSimpleMessage();
|
System.out.println("result......"+jsonNode.toString());
|
} catch (UnirestException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
*/
|
|
|
//////邮件模板测试发送
|
ClientResponse clientResponse = latchTest.SendSimpleMessage();
|
System.out.println(clientResponse.getStatus());
|
|
|
//////并发邮件发送测试
|
/*
|
Runnable taskTemp = new Runnable() {
|
|
// 注意,此处是非线程安全的,留坑
|
private int iCounter;
|
|
@Override
|
public void run() {
|
for(int i = 0; i < 5; i++) {
|
|
JsonNode jsonNode;
|
try {
|
jsonNode = latchTest.sendSimpleMessage();
|
System.out.println("result......"+jsonNode.toString());
|
} catch (UnirestException e1) {
|
// TODO Auto-generated catch block
|
e1.printStackTrace();
|
}
|
|
iCounter++;
|
System.out.println(System.nanoTime() + " [" + Thread.currentThread().getName() + "] iCounter = " + iCounter);
|
try {
|
Thread.sleep(100);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
|
}
|
}
|
};
|
|
try {
|
latchTest.startTaskAllInOnce(5, taskTemp);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
*/
|
|
}
|
|
|
public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
|
final CountDownLatch startGate = new CountDownLatch(1);
|
final CountDownLatch endGate = new CountDownLatch(threadNums);
|
for(int i = 0; i < threadNums; i++) {
|
Thread t = new Thread() {
|
public void run() {
|
try {
|
// 使线程在此等待,当开始门打开时,一起涌入门中
|
startGate.await();
|
try {
|
task.run();
|
} finally {
|
// 将结束门减1,减到0时,就可以开启结束门了
|
endGate.countDown();
|
}
|
} catch (InterruptedException ie) {
|
ie.printStackTrace();
|
}
|
}
|
};
|
t.start();
|
}
|
long startTime = System.nanoTime();
|
System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
|
// 因开启门只需一个开关,所以立马就开启开始门
|
startGate.countDown();
|
// 等等结束门开启
|
endGate.await();
|
long endTime = System.nanoTime();
|
System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
|
return endTime - startTime;
|
}
|
|
}
|