diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java index d077dcf65c21..037f6e775d8c 100644 --- a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java +++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java @@ -47,11 +47,15 @@ public class Constants { public static final String MAIL_SENDER = "mail.sender"; + public static final String MAIL_USER = "mail.user"; + public static final String MAIL_PASSWD = "mail.passwd"; public static final String XLS_FILE_PATH = "xls.file.path"; - public static final String MAIL_HOST = "mail.host"; + public static final String MAIL_HOST = "mail.smtp.host"; + + public static final String MAIL_PORT = "mail.smtp.port"; public static final String MAIL_SMTP_AUTH = "mail.smtp.auth"; @@ -61,6 +65,8 @@ public class Constants { public static final String MAIL_SMTP_SSL_ENABLE = "mail.smtp.ssl.enable"; + public static final String MAIL_SMTP_SSL_TRUST="mail.smtp.ssl.trust"; + public static final String TEXT_HTML_CHARSET_UTF_8 = "text/html;charset=utf-8"; public static final String STRING_TRUE = "true"; diff --git a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/MailUtils.java b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/MailUtils.java index 50d161f019d2..82add04daf0c 100644 --- a/escheduler-alert/src/main/java/cn/escheduler/alert/utils/MailUtils.java +++ b/escheduler-alert/src/main/java/cn/escheduler/alert/utils/MailUtils.java @@ -56,6 +56,8 @@ public class MailUtils { public static final String mailSender = getString(Constants.MAIL_SENDER); + public static final String mailUser = getString(Constants.MAIL_USER); + public static final String mailPasswd = getString(Constants.MAIL_PASSWD); public static final Boolean mailUseStartTLS = getBoolean(Constants.MAIL_SMTP_STARTTLS_ENABLE); @@ -68,6 +70,8 @@ public class MailUtils { public static final String sslEnable = getString(Constants.MAIL_SMTP_SSL_ENABLE); + public static final String sslTrust = getString(Constants.MAIL_SMTP_SSL_TRUST); + private static Template MAIL_TEMPLATE; static { @@ -126,16 +130,10 @@ public static Map sendMails(Collection receivers, Collect HtmlEmail email = new HtmlEmail(); try { - // set the SMTP sending server, 163 as follows: "smtp.163.com" - email.setHostName(mailServerHost); - email.setSmtpPort(mailServerPort); - //set charset + Session session = getSession(); + email.setMailSession(session); + email.setFrom(mailSender); email.setCharset(Constants.UTF_8); - // TLS verification - email.setTLS(Boolean.valueOf(starttlsEnable)); - - // SSL verification - email.setSSL(Boolean.valueOf(sslEnable)); if (CollectionUtils.isNotEmpty(receivers)){ // receivers mail for (String receiver : receivers) { @@ -286,23 +284,11 @@ private static MimeMessage getMimeMessage(Collection receivers) throws M // Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; - Properties props = new Properties(); - props.setProperty(Constants.MAIL_HOST, mailServerHost); - props.setProperty(Constants.MAIL_SMTP_AUTH, Constants.STRING_TRUE); - props.setProperty(Constants.MAIL_TRANSPORT_PROTOCOL, mailProtocol); - props.setProperty(Constants.MAIL_SMTP_STARTTLS_ENABLE, starttlsEnable); - props.setProperty("mail.smtp.ssl.enable", sslEnable); - Authenticator auth = new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - // mail username and password - return new PasswordAuthentication(mailSender, mailPasswd); - } - }; // 1. The first step in creating mail: creating session - Session session = Session.getInstance(props, auth); + Session session = getSession(); // Setting debug mode, can be turned off session.setDebug(false); + // 2. creating mail: Creating a MimeMessage MimeMessage msg = new MimeMessage(session); // 3. set sender @@ -314,6 +300,32 @@ protected PasswordAuthentication getPasswordAuthentication() { return msg; } + /** + * get session + * @return + */ + private static Session getSession() { + Properties props = new Properties(); + props.setProperty(Constants.MAIL_HOST, mailServerHost); + props.setProperty(Constants.MAIL_PORT, String.valueOf(mailServerPort)); + props.setProperty(Constants.MAIL_SMTP_AUTH, Constants.STRING_TRUE); + props.setProperty(Constants.MAIL_TRANSPORT_PROTOCOL, mailProtocol); + props.setProperty(Constants.MAIL_SMTP_STARTTLS_ENABLE, starttlsEnable); + props.setProperty(Constants.MAIL_SMTP_SSL_ENABLE, sslEnable); + props.setProperty(Constants.MAIL_SMTP_SSL_TRUST, sslTrust); + + Authenticator auth = new Authenticator() { + @Override + protected PasswordAuthentication getPasswordAuthentication() { + // mail username and password + return new PasswordAuthentication(mailUser, mailPasswd); + } + }; + + Session session = Session.getInstance(props, auth); + return session; + } + /** * * @param receiversCc @@ -370,13 +382,6 @@ private static void attachContent(Collection receiversCc, String title, * @throws EmailException */ private static Map getStringObjectMap(String title, String content, ShowType showType, Map retMap, HtmlEmail email) throws EmailException { - // sender's mailbox - email.setFrom(mailSender, mailSender); - /** - * if you need authentication information, set authentication: username-password. - * The registered name and password of the sender on the mail server respectively - */ - email.setAuthentication(mailSender, mailPasswd); /** * the subject of the message to be sent diff --git a/escheduler-alert/src/main/resources/alert.properties b/escheduler-alert/src/main/resources/alert.properties index 87ccae6377a0..096c60b4d8e6 100644 --- a/escheduler-alert/src/main/resources/alert.properties +++ b/escheduler-alert/src/main/resources/alert.properties @@ -3,15 +3,17 @@ alert.type=EMAIL # mail server configuration mail.protocol=SMTP -mail.server.host=smtp.exmail.qq.com +mail.server.host=xxx.xxx.com mail.server.port=25 -mail.sender=xxxxxxx -mail.passwd=xxxxxxx +mail.sender=xxx@xxx.com +mail.user=xxx@xxx.com +mail.passwd=111111 # TLS -mail.smtp.starttls.enable=false +mail.smtp.starttls.enable=true # SSL -mail.smtp.ssl.enable=true +mail.smtp.ssl.enable=false +mail.smtp.ssl.trust=xxx.xxx.com #xls file path,need create if not exist xls.file.path=/tmp/xls