Commit 5774b5f56b29c6d17f5e8fd43bd25cf13e12b559
1 parent
2a8a3289
Exists in
master
add email and weixin support
Showing
4 changed files
with
235 additions
and
5 deletions
Show diff stats
build.gradle
... | ... | @@ -27,7 +27,10 @@ dependencies { |
27 | 27 | "com.squareup.okhttp3:okhttp:3.14.1", |
28 | 28 | "com.belerweb:pinyin4j:2.5.1", |
29 | 29 | "org.slf4j:slf4j-api:1.7.28", |
30 | - "net.sf.json-lib:json-lib:2.2.3:jdk15" | |
30 | + "net.sf.json-lib:json-lib:2.2.3:jdk15", | |
31 | + "javax.mail:mail:1.4.7", | |
32 | + "com.aliyun.mns:aliyun-sdk-mns:1.1.8", | |
33 | + "com.alibaba:fastjson:1.2.72" | |
31 | 34 | ) |
32 | 35 | } |
33 | 36 | |
... | ... | @@ -59,7 +62,7 @@ uploadArchives { |
59 | 62 | authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) |
60 | 63 | } |
61 | 64 | pom.project { |
62 | - version '1.1.114' | |
65 | + version '1.1.116' | |
63 | 66 | artifactId ARTIFACT_Id |
64 | 67 | groupId GROUP_ID |
65 | 68 | packaging TYPE | ... | ... |
... | ... | @@ -0,0 +1,135 @@ |
1 | +package com.taover.util; | |
2 | + | |
3 | +import java.util.ArrayList; | |
4 | +import java.util.Arrays; | |
5 | +import java.util.Date; | |
6 | +import java.util.HashMap; | |
7 | +import java.util.List; | |
8 | +import java.util.Map; | |
9 | +import java.util.Properties; | |
10 | + | |
11 | +import javax.mail.Address; | |
12 | +import javax.mail.Authenticator; | |
13 | +import javax.mail.Message; | |
14 | +import javax.mail.PasswordAuthentication; | |
15 | +import javax.mail.Session; | |
16 | +import javax.mail.Transport; | |
17 | +import javax.mail.internet.InternetAddress; | |
18 | +import javax.mail.internet.MimeMessage; | |
19 | + | |
20 | +public class UtilEmail { | |
21 | + public static Map<String, String> GLOBAL_CONFIG = new HashMap<String, String>(){{ | |
22 | + put("host", "smtp.taover.com"); | |
23 | + put("port", "25"); | |
24 | + put("username", "taover-robot@taover.com"); | |
25 | + put("password", "Lexi@1798"); | |
26 | + put("header", "报警-服务不可用"); | |
27 | + put("from", "taover-robot@taover.com"); | |
28 | + put("fromName", "八爪云-服务监控"); | |
29 | + put("htmlTopic", "text/html; charset=utf-8"); | |
30 | + }}; | |
31 | + | |
32 | + | |
33 | + /** | |
34 | + * 发�?�HTML格式的邮�? | |
35 | + */ | |
36 | + public static void sendHtmlMail(List<String> toList, String subject, String htmlContent, Map<String, String> mailHostConfig) { | |
37 | +// 获取系统环境 | |
38 | + Properties prop = new Properties(); | |
39 | +// 添加必要的信�? | |
40 | + prop.put("mail.smtp.host", getValue("host", mailHostConfig)); | |
41 | + prop.put("mail.smtp.port", getValue("port", mailHostConfig)); | |
42 | + prop.put("mail.smtp.auth", "true"); | |
43 | + | |
44 | +// 设置对话和邮件服务器进行通讯 | |
45 | + Authenticator auth = new SimpleAuthenticator(getValue("username", mailHostConfig), getValue("password", mailHostConfig)); | |
46 | + Session session = Session.getDefaultInstance(prop, auth); | |
47 | + | |
48 | +// 设置邮件对象 | |
49 | + Message message = new MimeMessage(session); | |
50 | + try | |
51 | + { | |
52 | +// 设置邮件主题 | |
53 | + message.setSubject(subject); | |
54 | +// 设置邮件标题 | |
55 | + message.setHeader("Header", getValue("header", mailHostConfig)); | |
56 | +// 设置发�?�时�? | |
57 | + message.setSentDate(new Date()); | |
58 | + | |
59 | +// 设置发信人地�? �? 名字 | |
60 | + Address address = new InternetAddress(getValue("from", mailHostConfig), getValue("fromName", mailHostConfig)); | |
61 | +// 把发件人信息添加到信息中 | |
62 | + message.setFrom(address); | |
63 | + | |
64 | +// 设置收件人地�? | |
65 | + List<Address> addressList = new ArrayList<Address>(); | |
66 | + for(String item: toList) { | |
67 | + if(item != null && !"".equals(item.trim())) { | |
68 | + addressList.add(new InternetAddress(item)); | |
69 | + } | |
70 | + } | |
71 | + | |
72 | +// 设置接收人地�? | |
73 | + message.addRecipients(Message.RecipientType.TO, addressList.toArray(new Address[addressList.size()])); | |
74 | + | |
75 | +// 设置多个收件人地�? | |
76 | +// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com")); | |
77 | + | |
78 | +// 设置发�?�信息的内容 下面为发送hmml | |
79 | +// 设置邮件格式 | |
80 | + message.setContent(htmlContent, getValue("htmlTopic", mailHostConfig)); | |
81 | + | |
82 | +// 保存上面添加的信�? | |
83 | + message.saveChanges(); | |
84 | + Transport.send(message); | |
85 | + } catch (Exception e) { | |
86 | + e.printStackTrace(); | |
87 | + } | |
88 | + } | |
89 | + | |
90 | + public static void sendHtmlMail(List<String> toList, String subject, String htmlContent) { | |
91 | + sendHtmlMail(toList, subject, htmlContent, null); | |
92 | + } | |
93 | + | |
94 | + public static String getValue(String key, Map<String, String> localConfig) { | |
95 | + if(localConfig == null) { | |
96 | + return GLOBAL_CONFIG.get(key); | |
97 | + }else { | |
98 | + String data = localConfig.get(key); | |
99 | + if(data == null) { | |
100 | + return GLOBAL_CONFIG.get(key); | |
101 | + }else { | |
102 | + return data; | |
103 | + } | |
104 | + } | |
105 | + } | |
106 | + | |
107 | + /** | |
108 | + * @param args | |
109 | + */ | |
110 | + public static void main(String[] args) { | |
111 | + sendHtmlMail(Arrays.asList("wangbin@taover.com", "317058383@qq.com"), "测试邮件", "<h1>Hello Email Saturday V2</h1>"); | |
112 | + } | |
113 | +} | |
114 | + | |
115 | +class SimpleAuthenticator extends Authenticator{ | |
116 | + private String username=""; | |
117 | + private String password=""; | |
118 | + | |
119 | + public SimpleAuthenticator() { | |
120 | + super(); | |
121 | + } | |
122 | + | |
123 | + /** | |
124 | + * 设置验证的用户名和密�? | |
125 | + */ | |
126 | + public SimpleAuthenticator(String userName , String password) { | |
127 | + super(); | |
128 | + this.username = userName; | |
129 | + this.password = password; | |
130 | + } | |
131 | + protected PasswordAuthentication getPasswordAuthentication() { | |
132 | + return new PasswordAuthentication(this.username,this.password); | |
133 | + } | |
134 | + | |
135 | +} | |
0 | 136 | \ No newline at end of file | ... | ... |
src/main/java/com/taover/util/UtilJSON.java
... | ... | @@ -103,8 +103,8 @@ public class UtilJSON { |
103 | 103 | } |
104 | 104 | |
105 | 105 | public static void main(String[] args){ |
106 | - JSONObject data = JSONObject.fromObject("{\"message\":\"文字解析成功\",\"status\":true,\"statusCode\":\"null\",\"result\":{\"items\":[{\"district\":\"隆化县\",\"city\":\"承德市\",\"province\":\"河北省\",\"phone\":\"13621051230\",\"name\":{\"first_name\":\"wang\",\"second_name\":\"bin\",\"full_name\":null},\"address\":\"东阿超村\"}]}}"); | |
107 | - removeJsonNull(data); | |
108 | - System.out.println(data.toString()); | |
106 | +// JSONObject data = JSONObject.fromObject("{\"message\":\"文字解析成功\",\"status\":true,\"statusCode\":\"null\",\"result\":{\"items\":[{\"district\":\"隆化县\",\"city\":\"承德市\",\"province\":\"河北省\",\"phone\":\"13621051230\",\"name\":{\"first_name\":\"wang\",\"second_name\":\"bin\",\"full_name\":null},\"address\":\"东阿超村\"}]}}"); | |
107 | +// removeJsonNull(data); | |
108 | +// System.out.println(data.toString()); | |
109 | 109 | } |
110 | 110 | } | ... | ... |
... | ... | @@ -0,0 +1,92 @@ |
1 | +package com.taover.util; | |
2 | + | |
3 | +import java.util.HashMap; | |
4 | +import java.util.Map; | |
5 | + | |
6 | +import com.alibaba.fastjson.JSONObject; | |
7 | +import com.aliyun.mns.client.CloudAccount; | |
8 | +import com.aliyun.mns.client.CloudQueue; | |
9 | +import com.aliyun.mns.client.MNSClient; | |
10 | +import com.aliyun.mns.common.ClientException; | |
11 | +import com.aliyun.mns.common.ServiceException; | |
12 | +import com.aliyun.mns.model.Message; | |
13 | + | |
14 | +public class UtilWeixinMsg { | |
15 | + public static Map<String, String> GLOBAL_CONFIG = new HashMap<String, String>(){{ | |
16 | + put("appId", "c2MkSTjT8ghZ"); | |
17 | + put("secretKey", "7105ec065b8bb30fa6e3f13fccf92d0G"); | |
18 | + put("agentId", "47"); | |
19 | + put("tenantId", "11"); | |
20 | + put("alimnsQueueName", "oms-pc-msg-production"); | |
21 | + put("accessId", "H4fIVB56iHjR6zQw"); | |
22 | + put("accessKey", "7bA395UltFp16kWPJT7Pfz0XYXCk4Q"); | |
23 | + put("mnsEndpoint", "http://1225610490807748.mns.cn-hangzhou.aliyuncs.com"); | |
24 | + }}; | |
25 | + | |
26 | + //发�?�文本消�? | |
27 | + public static void sendTextMessage(String weixinId, String content, Map<String, String> alimnsConfig) throws Exception{ | |
28 | + Map<String, Object> data = new HashMap<String, Object>(); | |
29 | + data.put("appId", getValue("appId", alimnsConfig)); | |
30 | + String timestamp = System.currentTimeMillis()+""; | |
31 | + data.put("timestamp", timestamp); | |
32 | + data.put("signature", UtilEncrypt.MD5Lower32(getValue("appId", alimnsConfig) + timestamp + getValue("secretKey", alimnsConfig))); | |
33 | + data.put("agentId", getValue("agentId", alimnsConfig)); | |
34 | + data.put("toWxid", weixinId); | |
35 | + data.put("kind", "TEXT"); | |
36 | + data.put("content", content); | |
37 | + data.put("atList", ""); | |
38 | + data.put("merchantId", getValue("tenantId", alimnsConfig)); | |
39 | + | |
40 | + CloudAccount account = new CloudAccount(getValue("accessId", alimnsConfig), getValue("accessKey", alimnsConfig), getValue("mnsEndpoint", alimnsConfig)); | |
41 | + MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全 | |
42 | + try { | |
43 | + CloudQueue queue = client.getQueueRef(getValue("alimnsQueueName", alimnsConfig)); | |
44 | + Message message = new Message(); | |
45 | + message.setMessageBody(JSONObject.toJSONString(data)); | |
46 | + queue.putMessage(message); | |
47 | + }catch(ClientException ce){ | |
48 | + System.out.println("Something wrong with the network connection between client and MNS service." | |
49 | + + "Please check your network and DNS availablity."); | |
50 | + ce.printStackTrace(); | |
51 | + }catch(ServiceException se){ | |
52 | + se.printStackTrace(); | |
53 | + if (se.getErrorCode() != null){ | |
54 | + if (se.getErrorCode().equals("QueueNotExist")){ | |
55 | + System.out.println("Queue is not exist.Please create before use"); | |
56 | + }else if(se.getErrorCode().equals("TimeExpired")){ | |
57 | + System.out.println("The request is time expired. Please check your local machine timeclock"); | |
58 | + } | |
59 | + } | |
60 | + }catch(Exception e){ | |
61 | + System.out.println("Unknown exception happened!"); | |
62 | + e.printStackTrace(); | |
63 | + } | |
64 | + client.close(); // 程序�?出时,需主动调用client的close方法进行资源释放 | |
65 | + } | |
66 | + | |
67 | + public static void sendTextMessage(String weixinId, String content) throws Exception{ | |
68 | + sendTextMessage(weixinId, content, null); | |
69 | + } | |
70 | + | |
71 | + public static String getValue(String key, Map<String, String> localConfig) { | |
72 | + if(localConfig == null) { | |
73 | + return GLOBAL_CONFIG.get(key); | |
74 | + }else { | |
75 | + String data = localConfig.get(key); | |
76 | + if(data == null) { | |
77 | + return GLOBAL_CONFIG.get(key); | |
78 | + }else { | |
79 | + return data; | |
80 | + } | |
81 | + } | |
82 | + } | |
83 | + | |
84 | + public static void main(String args[]) { | |
85 | + try { | |
86 | + sendTextMessage("wxid_kn7w9ctq11ta21", "hello weixin v2"); | |
87 | + } catch (Exception e) { | |
88 | + // TODO Auto-generated catch block | |
89 | + e.printStackTrace(); | |
90 | + } | |
91 | + } | |
92 | +} | ... | ... |