1821349743@qq.com
2023-04-06 be4c913206aba510abb30ceeb145424f18407592
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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;
    }
    
}