Helius
2021-09-16 07048dc1608c84651e7a46dfe89f03f376b22ab6
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
package cc.mrbird.febs.common.utils;
 
 
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.TreeMap;
 
/**
 * 邮件发送
 *
 * @author wzy
 * @date 2020-05-27
 **/
public class SubMailSend {
 
    /**
     * 时间戳接口配置
     */
    public static final String TIMESTAMP = "https://api.mysubmail.com/service/timestamp";
    /**
     * API 请求接口配置
     */
    private static final String URL = "https://api.mysubmail.com/mail/xsend";
 
    public static final String TYPE_MD5 = "md5";
    public static final String TYPE_SHA1 = "sha1";
 
    private static final String APP_ID = "16082";
    private static final String APP_KEY = "f34c792a1112c16c190ed7190d386c4f";
    private static final String FROM = "hibit@submail.hibit.cc";
    private static final String SIGN_TYPE = "";
 
    /**
     * 发送验证码邮件
     *
     * @param to   对象
     * @param code 验证码
     * @return true or false
     */
    public static boolean sendMail(String to, String code) {
        JSONObject vars = new JSONObject();
        vars.put("code", code);
 
        String project = "CVj6P";
        return request(vars, project, to);
    }
 
    /**
     * 发送充值成功邮件
     *
     * @param to      对象
     * @param time    成功时间
     * @param orderNo 订单编号
     * @return true or false
     */
    public static boolean sendRechargeMail(String to, String time, String orderNo) {
        JSONObject vars = new JSONObject();
        vars.put("time", time);
        vars.put("orderNo", orderNo);
        String project = "4DvTC2";
        return request(vars, project, to);
    }
 
    public static boolean sendWithdrawalMail(String to, String time) {
        JSONObject vars = new JSONObject();
        vars.put("time", time);
        String project = "e3BO91";
        return request(vars, project, to);
    }
 
 
    private static boolean request(JSONObject vars, String project, String to) {
        TreeMap<String, Object> requestData = new TreeMap<String, Object>();
        requestData.put("appid", APP_ID);
        requestData.put("project", project);
        requestData.put("to", to);
        requestData.put("from", FROM);
        if (!vars.isEmpty()) {
            requestData.put("vars", vars.toString());
        }
 
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        @SuppressWarnings("deprecation")
        ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
        for (Map.Entry<String, Object> entry : requestData.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                builder.addTextBody(key, String.valueOf(value), contentType);
            }
        }
        if (SIGN_TYPE.equals(TYPE_MD5) || SIGN_TYPE.equals(TYPE_SHA1)) {
            String timestamp = getTimestamp();
            requestData.put("timestamp", timestamp);
            requestData.put("sign_type", SIGN_TYPE);
            String signStr = APP_ID + APP_KEY + RequestEncoder.formatRequest(requestData) + APP_ID + APP_KEY;
 
            builder.addTextBody("timestamp", timestamp);
            builder.addTextBody("sign_type", SIGN_TYPE);
            builder.addTextBody("signature", RequestEncoder.encode(SIGN_TYPE, signStr), contentType);
        } else {
            builder.addTextBody("signature", APP_KEY, contentType);
        }
 
        HttpPost httpPost = new HttpPost(URL);
        httpPost.addHeader("charset", "UTF-8");
        httpPost.setEntity(builder.build());
        try {
            CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
            HttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                String jsonStr = EntityUtils.toString(httpEntity, "UTF-8");
                if ("success".equals(JSONObject.fromObject(jsonStr).getString("status"))) {
                    return true;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 
 
    /**
     * 获取时间戳
     *
     * @return
     */
    private static String getTimestamp() {
        CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
        HttpGet httpget = new HttpGet(TIMESTAMP);
        try {
            HttpResponse response = closeableHttpClient.execute(httpget);
            HttpEntity httpEntity = response.getEntity();
            String jsonStr = EntityUtils.toString(httpEntity, "UTF-8");
            if (jsonStr != null) {
                JSONObject json = JSONObject.fromObject(jsonStr);
                return json.getString("timestamp");
            }
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return null;
    }
 
    public static void main(String[] args) {
//        System.out.println(sendMail("546766039@qq.com", "123456"));
 
        System.out.println(sendRechargeMail("546766039@qq.com", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN), "123456"));
    }
}