|
|
@@ -1,84 +1,52 @@
|
|
|
package cn.reghao.autodop.dmaster.utils.notifier;
|
|
|
|
|
|
-import javax.mail.Session;
|
|
|
-import javax.mail.Transport;
|
|
|
-import javax.mail.internet.InternetAddress;
|
|
|
-import javax.mail.internet.MimeMessage;
|
|
|
-import java.io.UnsupportedEncodingException;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.Properties;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
* @date 2020-05-05 12:46:30
|
|
|
*/
|
|
|
public class EmailNotify implements Notify {
|
|
|
- private InternetAddress from;
|
|
|
- private Session session;
|
|
|
- private String emailAccount;
|
|
|
- private String emailPassword;
|
|
|
+ private EmailSender sender;
|
|
|
|
|
|
- public EmailNotify(String emailAccount, String emailPassword, String emailSmtp) throws Exception {
|
|
|
- this.emailAccount = emailAccount;
|
|
|
- this.emailPassword = emailPassword;
|
|
|
- this.from = new InternetAddress(emailAccount, "admin@emall", "UTF-8");
|
|
|
- initSessioin(emailSmtp);
|
|
|
- }
|
|
|
-
|
|
|
- private void initSessioin(String emailSmtp) {
|
|
|
- Properties props = new Properties();
|
|
|
- props.setProperty("mail.transport.protocol", "smtp");
|
|
|
- props.setProperty("mail.smtp.host", emailSmtp);
|
|
|
- props.setProperty("mail.smtp.auth", "true");
|
|
|
-
|
|
|
- session = Session.getInstance(props);
|
|
|
- session.setDebug(true);
|
|
|
+ public EmailNotify(String account, String password, String smtp, String sender) throws Exception {
|
|
|
+ this.sender = new EmailSender(account, password, smtp, sender);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void send(String dest, Object content) {
|
|
|
- try {
|
|
|
- MimeMessage message = message(emailDestinations(dest), "来自远方的问候", (String) content);
|
|
|
- Transport transport = session.getTransport();
|
|
|
- transport.connect(emailAccount, emailPassword);
|
|
|
- transport.sendMessage(message, message.getAllRecipients());
|
|
|
- transport.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private InternetAddress[] emailDestinations(String dest) throws UnsupportedEncodingException {
|
|
|
- String[] strs = dest.split(",");
|
|
|
- int len = strs.length;
|
|
|
- InternetAddress[] addresses = new InternetAddress[len];
|
|
|
- for (int i = 0; i < len; i++) {
|
|
|
- addresses[i] = new InternetAddress(strs[i], "", "UTF-8");
|
|
|
- }
|
|
|
-
|
|
|
- return addresses;
|
|
|
- }
|
|
|
-
|
|
|
- private MimeMessage message(InternetAddress[] addresses, String subject, String content) throws Exception {
|
|
|
- MimeMessage message = new MimeMessage(session);
|
|
|
- message.setFrom(from);
|
|
|
- message.setRecipients(MimeMessage.RecipientType.TO, addresses);
|
|
|
- message.setSubject(subject, "UTF-8");
|
|
|
- message.setContent(content, "text/html;charset=UTF-8");
|
|
|
- message.setSentDate(new Date());
|
|
|
-
|
|
|
- return message;
|
|
|
+ public void send(String receiver, Object msg) throws Exception {
|
|
|
+ EmailMessage emailMessage = (EmailMessage) msg;
|
|
|
+ sender.send(receiver, emailMessage.getSubject(), emailMessage.getContent());
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("account", "gisgit@163.com");
|
|
|
+ map.put("password", "gjs@gsh1990");
|
|
|
+ map.put("smtp", "smtp.163.com");
|
|
|
+ map.put("sender", "devops@iquizoo");
|
|
|
+
|
|
|
String account = "gisgit@163.com";
|
|
|
String password = "gjs@gsh1990";
|
|
|
String smtp = "smtp.163.com";
|
|
|
- EmailNotify emailNotify = new EmailNotify(account, password, smtp);
|
|
|
+ String sender = "devops@iquizoo";
|
|
|
+ EmailNotify emailNotify = new EmailNotify(account, password, smtp, sender);
|
|
|
|
|
|
- String dest = "506193266@qq.com";
|
|
|
+ List<String> receivers = new ArrayList<>();
|
|
|
+ receivers.add("506193266@qq.com");
|
|
|
+ receivers.add("0xffxyz@gmail.com");
|
|
|
+
|
|
|
+ //String receiver = "506193266@qq.com";
|
|
|
+ EmailMessage message = new EmailMessage();
|
|
|
+ message.setSubject("可靠消息");
|
|
|
String content = "据可靠消息,火箭军将于本月 15 日在西北试射一枚 DF-41 导弹,目的地位于美国西部外海。";
|
|
|
- content = "据可靠消息,火箭军将于本月 15 日在黄海外海试射一枚巨浪 3 型导弹,目的地位于美国西部外海。";
|
|
|
- emailNotify.send(dest, content);
|
|
|
+ message.setContent(content);
|
|
|
+
|
|
|
+ for (String receiver : receivers) {
|
|
|
+ emailNotify.send(receiver, message);
|
|
|
+ }
|
|
|
}
|
|
|
}
|