Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue#728 #746

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -126,16 +130,10 @@ public static Map<String,Object> sendMails(Collection<String> 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) {
Expand Down Expand Up @@ -286,23 +284,11 @@ private static MimeMessage getMimeMessage(Collection<String> 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
Expand All @@ -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
Expand Down Expand Up @@ -370,13 +382,6 @@ private static void attachContent(Collection<String> receiversCc, String title,
* @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
Expand Down
12 changes: 7 additions & 5 deletions escheduler-alert/src/main/resources/alert.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down