Browse Source

issue#728 (#746)

The mailSender in PasswordAuthentication() and setFrom() may not be the same in actual use, and it's best to set it up separately. please add a example about this.
pull/2/head
LiemLin 5 years ago committed by lgcareer
parent
commit
78d4ef9662
  1. 8
      escheduler-alert/src/main/java/cn/escheduler/alert/utils/Constants.java
  2. 65
      escheduler-alert/src/main/java/cn/escheduler/alert/utils/MailUtils.java
  3. 12
      escheduler-alert/src/main/resources/alert.properties

8
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_SENDER = "mail.sender";
public static final String MAIL_USER = "mail.user";
public static final String MAIL_PASSWD = "mail.passwd"; public static final String MAIL_PASSWD = "mail.passwd";
public static final String XLS_FILE_PATH = "xls.file.path"; 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"; 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_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 TEXT_HTML_CHARSET_UTF_8 = "text/html;charset=utf-8";
public static final String STRING_TRUE = "true"; public static final String STRING_TRUE = "true";

65
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 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 String mailPasswd = getString(Constants.MAIL_PASSWD);
public static final Boolean mailUseStartTLS = getBoolean(Constants.MAIL_SMTP_STARTTLS_ENABLE); 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 sslEnable = getString(Constants.MAIL_SMTP_SSL_ENABLE);
public static final String sslTrust = getString(Constants.MAIL_SMTP_SSL_TRUST);
private static Template MAIL_TEMPLATE; private static Template MAIL_TEMPLATE;
static { static {
@ -126,16 +130,10 @@ public class MailUtils {
HtmlEmail email = new HtmlEmail(); HtmlEmail email = new HtmlEmail();
try { try {
// set the SMTP sending server, 163 as follows: "smtp.163.com" Session session = getSession();
email.setHostName(mailServerHost); email.setMailSession(session);
email.setSmtpPort(mailServerPort); email.setFrom(mailSender);
//set charset
email.setCharset(Constants.UTF_8); email.setCharset(Constants.UTF_8);
// TLS verification
email.setTLS(Boolean.valueOf(starttlsEnable));
// SSL verification
email.setSSL(Boolean.valueOf(sslEnable));
if (CollectionUtils.isNotEmpty(receivers)){ if (CollectionUtils.isNotEmpty(receivers)){
// receivers mail // receivers mail
for (String receiver : receivers) { for (String receiver : receivers) {
@ -286,23 +284,11 @@ public class MailUtils {
// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); // Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
// final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; // 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 // 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 // Setting debug mode, can be turned off
session.setDebug(false); session.setDebug(false);
// 2. creating mail: Creating a MimeMessage // 2. creating mail: Creating a MimeMessage
MimeMessage msg = new MimeMessage(session); MimeMessage msg = new MimeMessage(session);
// 3. set sender // 3. set sender
@ -314,6 +300,32 @@ public class MailUtils {
return msg; 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 * @param receiversCc
@ -370,13 +382,6 @@ public class MailUtils {
* @throws EmailException * @throws EmailException
*/ */
private static Map<String, Object> getStringObjectMap(String title, String content, ShowType showType, Map<String, Object> retMap, HtmlEmail email) throws EmailException { private static Map<String, Object> getStringObjectMap(String title, String content, ShowType showType, Map<String, Object> 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 * the subject of the message to be sent

12
escheduler-alert/src/main/resources/alert.properties

@ -3,15 +3,17 @@ alert.type=EMAIL
# mail server configuration # mail server configuration
mail.protocol=SMTP mail.protocol=SMTP
mail.server.host=smtp.exmail.qq.com mail.server.host=xxx.xxx.com
mail.server.port=25 mail.server.port=25
mail.sender=xxxxxxx mail.sender=xxx@xxx.com
mail.passwd=xxxxxxx mail.user=xxx@xxx.com
mail.passwd=111111
# TLS # TLS
mail.smtp.starttls.enable=false mail.smtp.starttls.enable=true
# SSL # 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,need create if not exist
xls.file.path=/tmp/xls xls.file.path=/tmp/xls

Loading…
Cancel
Save