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