diff --git a/build.gradle b/build.gradle index cb06d61..2417707 100644 --- a/build.gradle +++ b/build.gradle @@ -27,7 +27,10 @@ dependencies { "com.squareup.okhttp3:okhttp:3.14.1", "com.belerweb:pinyin4j:2.5.1", "org.slf4j:slf4j-api:1.7.28", - "net.sf.json-lib:json-lib:2.2.3:jdk15" + "net.sf.json-lib:json-lib:2.2.3:jdk15", + "javax.mail:mail:1.4.7", + "com.aliyun.mns:aliyun-sdk-mns:1.1.8", + "com.alibaba:fastjson:1.2.72" ) } @@ -59,7 +62,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '1.1.114' + version '1.1.116' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/util/UtilEmail.java b/src/main/java/com/taover/util/UtilEmail.java new file mode 100644 index 0000000..481d5d2 --- /dev/null +++ b/src/main/java/com/taover/util/UtilEmail.java @@ -0,0 +1,135 @@ +package com.taover.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import javax.mail.Address; +import javax.mail.Authenticator; +import javax.mail.Message; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +public class UtilEmail { + public static Map GLOBAL_CONFIG = new HashMap(){{ + put("host", "smtp.taover.com"); + put("port", "25"); + put("username", "taover-robot@taover.com"); + put("password", "Lexi@1798"); + put("header", "报警-服务不可用"); + put("from", "taover-robot@taover.com"); + put("fromName", "八爪云-服务监控"); + put("htmlTopic", "text/html; charset=utf-8"); + }}; + + + /** + * 发�?�HTML格式的邮�? + */ + public static void sendHtmlMail(List toList, String subject, String htmlContent, Map mailHostConfig) { +// 获取系统环境 + Properties prop = new Properties(); +// 添加必要的信�? + prop.put("mail.smtp.host", getValue("host", mailHostConfig)); + prop.put("mail.smtp.port", getValue("port", mailHostConfig)); + prop.put("mail.smtp.auth", "true"); + +// 设置对话和邮件服务器进行通讯 + Authenticator auth = new SimpleAuthenticator(getValue("username", mailHostConfig), getValue("password", mailHostConfig)); + Session session = Session.getDefaultInstance(prop, auth); + +// 设置邮件对象 + Message message = new MimeMessage(session); + try + { +// 设置邮件主题 + message.setSubject(subject); +// 设置邮件标题 + message.setHeader("Header", getValue("header", mailHostConfig)); +// 设置发�?�时�? + message.setSentDate(new Date()); + +// 设置发信人地�? �? 名字 + Address address = new InternetAddress(getValue("from", mailHostConfig), getValue("fromName", mailHostConfig)); +// 把发件人信息添加到信息中 + message.setFrom(address); + +// 设置收件人地�? + List
addressList = new ArrayList
(); + for(String item: toList) { + if(item != null && !"".equals(item.trim())) { + addressList.add(new InternetAddress(item)); + } + } + +// 设置接收人地�? + message.addRecipients(Message.RecipientType.TO, addressList.toArray(new Address[addressList.size()])); + +// 设置多个收件人地�? +// message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com")); + +// 设置发�?�信息的内容 下面为发送hmml +// 设置邮件格式 + message.setContent(htmlContent, getValue("htmlTopic", mailHostConfig)); + +// 保存上面添加的信�? + message.saveChanges(); + Transport.send(message); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void sendHtmlMail(List toList, String subject, String htmlContent) { + sendHtmlMail(toList, subject, htmlContent, null); + } + + public static String getValue(String key, Map localConfig) { + if(localConfig == null) { + return GLOBAL_CONFIG.get(key); + }else { + String data = localConfig.get(key); + if(data == null) { + return GLOBAL_CONFIG.get(key); + }else { + return data; + } + } + } + + /** + * @param args + */ + public static void main(String[] args) { + sendHtmlMail(Arrays.asList("wangbin@taover.com", "317058383@qq.com"), "测试邮件", "

Hello Email Saturday V2

"); + } +} + +class SimpleAuthenticator extends Authenticator{ + private String username=""; + private String password=""; + + public SimpleAuthenticator() { + super(); + } + + /** + * 设置验证的用户名和密�? + */ + public SimpleAuthenticator(String userName , String password) { + super(); + this.username = userName; + this.password = password; + } + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(this.username,this.password); + } + +} \ No newline at end of file diff --git a/src/main/java/com/taover/util/UtilJSON.java b/src/main/java/com/taover/util/UtilJSON.java index 6172f3c..3636516 100644 --- a/src/main/java/com/taover/util/UtilJSON.java +++ b/src/main/java/com/taover/util/UtilJSON.java @@ -103,8 +103,8 @@ public class UtilJSON { } public static void main(String[] args){ - 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\":\"东阿超村\"}]}}"); - removeJsonNull(data); - System.out.println(data.toString()); +// 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\":\"东阿超村\"}]}}"); +// removeJsonNull(data); +// System.out.println(data.toString()); } } diff --git a/src/main/java/com/taover/util/UtilWeixinMsg.java b/src/main/java/com/taover/util/UtilWeixinMsg.java new file mode 100644 index 0000000..a5aed77 --- /dev/null +++ b/src/main/java/com/taover/util/UtilWeixinMsg.java @@ -0,0 +1,92 @@ +package com.taover.util; + +import java.util.HashMap; +import java.util.Map; + +import com.alibaba.fastjson.JSONObject; +import com.aliyun.mns.client.CloudAccount; +import com.aliyun.mns.client.CloudQueue; +import com.aliyun.mns.client.MNSClient; +import com.aliyun.mns.common.ClientException; +import com.aliyun.mns.common.ServiceException; +import com.aliyun.mns.model.Message; + +public class UtilWeixinMsg { + public static Map GLOBAL_CONFIG = new HashMap(){{ + put("appId", "c2MkSTjT8ghZ"); + put("secretKey", "7105ec065b8bb30fa6e3f13fccf92d0G"); + put("agentId", "47"); + put("tenantId", "11"); + put("alimnsQueueName", "oms-pc-msg-production"); + put("accessId", "H4fIVB56iHjR6zQw"); + put("accessKey", "7bA395UltFp16kWPJT7Pfz0XYXCk4Q"); + put("mnsEndpoint", "http://1225610490807748.mns.cn-hangzhou.aliyuncs.com"); + }}; + + //发�?�文本消�? + public static void sendTextMessage(String weixinId, String content, Map alimnsConfig) throws Exception{ + Map data = new HashMap(); + data.put("appId", getValue("appId", alimnsConfig)); + String timestamp = System.currentTimeMillis()+""; + data.put("timestamp", timestamp); + data.put("signature", UtilEncrypt.MD5Lower32(getValue("appId", alimnsConfig) + timestamp + getValue("secretKey", alimnsConfig))); + data.put("agentId", getValue("agentId", alimnsConfig)); + data.put("toWxid", weixinId); + data.put("kind", "TEXT"); + data.put("content", content); + data.put("atList", ""); + data.put("merchantId", getValue("tenantId", alimnsConfig)); + + CloudAccount account = new CloudAccount(getValue("accessId", alimnsConfig), getValue("accessKey", alimnsConfig), getValue("mnsEndpoint", alimnsConfig)); + MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全 + try { + CloudQueue queue = client.getQueueRef(getValue("alimnsQueueName", alimnsConfig)); + Message message = new Message(); + message.setMessageBody(JSONObject.toJSONString(data)); + queue.putMessage(message); + }catch(ClientException ce){ + System.out.println("Something wrong with the network connection between client and MNS service." + + "Please check your network and DNS availablity."); + ce.printStackTrace(); + }catch(ServiceException se){ + se.printStackTrace(); + if (se.getErrorCode() != null){ + if (se.getErrorCode().equals("QueueNotExist")){ + System.out.println("Queue is not exist.Please create before use"); + }else if(se.getErrorCode().equals("TimeExpired")){ + System.out.println("The request is time expired. Please check your local machine timeclock"); + } + } + }catch(Exception e){ + System.out.println("Unknown exception happened!"); + e.printStackTrace(); + } + client.close(); // 程序�?出时,需主动调用client的close方法进行资源释放 + } + + public static void sendTextMessage(String weixinId, String content) throws Exception{ + sendTextMessage(weixinId, content, null); + } + + public static String getValue(String key, Map localConfig) { + if(localConfig == null) { + return GLOBAL_CONFIG.get(key); + }else { + String data = localConfig.get(key); + if(data == null) { + return GLOBAL_CONFIG.get(key); + }else { + return data; + } + } + } + + public static void main(String args[]) { + try { + sendTextMessage("wxid_kn7w9ctq11ta21", "hello weixin v2"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} -- libgit2 0.21.2