admin
2022-03-25 17055fd8d36504b79a5def28f5d4b4740faf012d
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
package org.yeshi.utils.mail;
 
import net.sf.json.JSONObject;
import org.yeshi.utils.HttpUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.encrypt.AESUtil;
import org.yeshi.utils.exception.MailSendException;
 
import java.util.*;
 
/**
 * @author hxh
 * @title: EmailApiUtil
 * @description: 邮件帮助
 * @date 2022/3/15 17:04
 */
public class EmailApiUtil {
 
    final static String AESKEY = "123bN5%M7*~a8888";
    final static String SIGNKEY = "EmailYeshi@88889!~";
    final static String API_SEND = "http://api.mail.yeshitv.com:8081/email/send";
 
 
    /**
     * @return void
     * @author hxh
     * @description 邮件发送接口
     * @date 18:13 2022/3/15
     * @param: emailInfo
     **/
    public static void sendEmail(EmailInfo emailInfo) throws MailSendException {
        JSONObject accountJSON = new JSONObject();
        accountJSON.put("toEmail", emailInfo.getToEmail());
        accountJSON.put("fromEmail", emailInfo.getFromEmail());
        accountJSON.put("fromEmailPwd", emailInfo.getFormEmailPwd());
        Map<String, String> params = getBaseParams();
        params.put("title", emailInfo.getTitle());
        params.put("content", emailInfo.getContent());
        params.put("app", emailInfo.getApp());
        params.put("accountInfo", AESUtil.encrypt(accountJSON.toString(), AESKEY));
        params.put("sign", getSign(params));
 
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
 
        try {
            String result = HttpUtil.post(API_SEND, params, headers);
            if (StringUtil.isNullOrEmpty(result)) {
                throw new Exception("内容返回为空");
            }
            JSONObject resultJSON = JSONObject.fromObject(result);
            if (resultJSON.optInt("code") != 0) {
                throw new MailSendException(MailSendException.CODE_BUSINESS_ERROR, resultJSON.optString("msg"));
            }
        } catch (MailSendException e) {
            throw e;
        } catch (Exception e) {
            throw new MailSendException(MailSendException.CODE_NETWORK_ERROR, "接口网络出错");
        }
    }
 
    private static Map<String, String> getBaseParams() {
        Map<String, String> params = new HashMap<>();
        params.put("timestamp", System.currentTimeMillis() + "");
        return params;
    }
 
    private static String getSign(Map<String, String> params) {
        List<String> list = new ArrayList<>();
        for (Iterator<String> its = params.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
            if ("sign".equalsIgnoreCase(key))
                continue;
            list.add(key + "=" + params.get(key));
        }
 
        Collections.sort(list);
        String str = StringUtil.concat(list, "&");
        str += SIGNKEY;
        String sign = StringUtil.Md5(str);
        return sign;
    }
 
//    public static void main(String[] args) {
//
//        //"he15901227708@163.com", "hexiaohui1011", "hexiaohui@banliapp.com", "测试", "测试内容"
//        EmailInfo emailInfo = new EmailInfo();
//        emailInfo.setApp("test");
//        emailInfo.setContent("测试内容");
//        emailInfo.setFormEmailPwd("hexiaohui1011");
//        emailInfo.setFromEmail("he15901227708@163.com");
//        emailInfo.setTitle("测试");
//        emailInfo.setToEmail("hexiaohui@banliapp.com");
//
//        try {
//            EmailApiUtil.sendEmail(emailInfo);
//        } catch (MailSendException e) {
//            e.printStackTrace();
//        }
//
//    }
 
 
}