From edf211029ca6b07962d1fb37a18bb184bf89acdd Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Wed, 15 Jul 2020 15:43:22 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/modules/Sms106Send.java | 4 src/main/java/com/xcong/excoin/common/utils/SubMailSend.java | 169 +++++++++++++++++++++++++++++++++ pom.xml | 34 ++++++ src/main/java/com/xcong/excoin/common/utils/RequestEncoder.java | 71 ++++++++++++++ 4 files changed, 277 insertions(+), 1 deletions(-) diff --git a/pom.xml b/pom.xml index f8db766..9d2b5f2 100644 --- a/pom.xml +++ b/pom.xml @@ -214,6 +214,40 @@ <artifactId>xml-apis</artifactId> <version>1.4.01</version> </dependency> + + <!-- submail邮件 start --> + <dependency> + <groupId>commons-logging</groupId> + <artifactId>commons-logging</artifactId> + <version>1.1.1</version> + </dependency> + <dependency> + <groupId>commons-collections</groupId> + <artifactId>commons-collections</artifactId> + <version>3.2.1</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.3.2</version> + </dependency> + <dependency> + <groupId>net.sf.ezmorph</groupId> + <artifactId>ezmorph</artifactId> + <version>1.0.3</version> + </dependency> + <dependency> + <groupId>net.sf.json-lib</groupId> + <artifactId>json-lib</artifactId> + <version>2.2.3</version> + <classifier>jdk15</classifier> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpmime</artifactId> + <version>4.3.5</version> + </dependency> + <!-- submail邮件 end --> </dependencies> <build> diff --git a/src/main/java/com/xcong/excoin/common/utils/RequestEncoder.java b/src/main/java/com/xcong/excoin/common/utils/RequestEncoder.java new file mode 100644 index 0000000..f497891 --- /dev/null +++ b/src/main/java/com/xcong/excoin/common/utils/RequestEncoder.java @@ -0,0 +1,71 @@ +package com.xcong.excoin.common.utils; + +import java.security.MessageDigest; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * 处理请求数据 + * @author submail + * + */ +public class RequestEncoder { + + private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', + '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + public static final String MD5 = "MD5"; + public static final String SHA1 = "SHA1"; + /** + * 编码的字符串 + * + * @param algorithm + * @param str + * @return String + */ + public static String encode(String algorithm, String str) { + if (str == null) { + return null; + } + try { + MessageDigest messageDigest = MessageDigest.getInstance(algorithm); + messageDigest.update(str.getBytes("UTF-8")); + return getFormattedText(messageDigest.digest()); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } + + /** + *获取原始字节并将其格式化。 + * @param bytes + * the raw bytes from the digest. + * @return the formatted bytes. + */ + private static String getFormattedText(byte[] bytes) { + int len = bytes.length; + StringBuilder buf = new StringBuilder(len * 2); + for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); + buf.append(HEX_DIGITS[bytes[j] & 0x0f]); + } + return buf.toString(); + } + + public static String formatRequest(Map<String, Object> data){ + Set<String> keySet = data.keySet(); + Iterator<String> it = keySet.iterator(); + StringBuffer sb = new StringBuffer(); + while(it.hasNext()){ + String key = it.next(); + Object value = data.get(key); + if(value instanceof String){ + sb.append(key + "=" + value + "&"); + } + } + if(sb.length() != 0){ + return sb.substring(0, sb.length() - 1); + } + return null; + } +} diff --git a/src/main/java/com/xcong/excoin/common/utils/SubMailSend.java b/src/main/java/com/xcong/excoin/common/utils/SubMailSend.java new file mode 100644 index 0000000..95b7356 --- /dev/null +++ b/src/main/java/com/xcong/excoin/common/utils/SubMailSend.java @@ -0,0 +1,169 @@ +package com.xcong.excoin.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 = "biue@submail.biue.me"; + 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 = "zoKVB"; + 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 = "x820C2"; + 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")); + } +} diff --git a/src/main/java/com/xcong/excoin/modules/Sms106Send.java b/src/main/java/com/xcong/excoin/modules/Sms106Send.java index a213e6b..3fc783a 100644 --- a/src/main/java/com/xcong/excoin/modules/Sms106Send.java +++ b/src/main/java/com/xcong/excoin/modules/Sms106Send.java @@ -44,7 +44,9 @@ String content = StrUtil.format(msg, time, orderNo); return request(phone, content, "提现"); } - public static boolean sendWithdrawalCoinMsg(String phone, String time) { + public static boolean + + sendWithdrawalCoinMsg(String phone, String time) { String msg = "尊敬的用户,您的帐号于{}有一笔成功提现订单,如有疑问请联系客服。"; String content = StrUtil.format(msg, time); return request(phone, content, "提币"); -- Gitblit v1.9.1