|
|
|
@ -14,23 +14,39 @@
|
|
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
|
* limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
package org.apache.dolphinscheduler.alert.utils; |
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.alert.template.AlertTemplate; |
|
|
|
|
import org.apache.dolphinscheduler.alert.template.AlertTemplateFactory; |
|
|
|
|
import org.apache.dolphinscheduler.common.enums.ShowType; |
|
|
|
|
import org.apache.commons.mail.EmailException; |
|
|
|
|
import org.apache.commons.mail.HtmlEmail; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.CollectionUtils; |
|
|
|
|
import org.apache.dolphinscheduler.common.utils.StringUtils; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import javax.mail.*; |
|
|
|
|
import javax.mail.internet.*; |
|
|
|
|
import java.io.*; |
|
|
|
|
import java.util.*; |
|
|
|
|
import org.apache.commons.mail.EmailException; |
|
|
|
|
import org.apache.commons.mail.HtmlEmail; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.Collection; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
|
|
|
|
import javax.mail.Authenticator; |
|
|
|
|
import javax.mail.Message; |
|
|
|
|
import javax.mail.MessagingException; |
|
|
|
|
import javax.mail.PasswordAuthentication; |
|
|
|
|
import javax.mail.Session; |
|
|
|
|
import javax.mail.Transport; |
|
|
|
|
import javax.mail.internet.InternetAddress; |
|
|
|
|
import javax.mail.internet.MimeBodyPart; |
|
|
|
|
import javax.mail.internet.MimeMessage; |
|
|
|
|
import javax.mail.internet.MimeMultipart; |
|
|
|
|
import javax.mail.internet.MimeUtility; |
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* mail utils |
|
|
|
@ -55,7 +71,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
public static final Boolean MAIL_USE_SSL = PropertyUtils.getBoolean(Constants.MAIL_SMTP_SSL_ENABLE); |
|
|
|
|
|
|
|
|
|
public static final String xlsFilePath = PropertyUtils.getString(Constants.XLS_FILE_PATH,"/tmp/xls"); |
|
|
|
|
public static final String XLS_FILE_PATH = PropertyUtils.getString(Constants.XLS_FILE_PATH, "/tmp/xls"); |
|
|
|
|
|
|
|
|
|
public static final String STARTTLS_ENABLE = PropertyUtils.getString(Constants.MAIL_SMTP_STARTTLS_ENABLE); |
|
|
|
|
|
|
|
|
@ -70,8 +86,13 @@ public class MailUtils {
|
|
|
|
|
System.setProperty("mail.mime.splitlongparameters", "false"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private MailUtils() { |
|
|
|
|
throw new IllegalStateException(MailUtils.class.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* send mail to receivers |
|
|
|
|
* |
|
|
|
|
* @param receivers the receiver list |
|
|
|
|
* @param title the title |
|
|
|
|
* @param content the content |
|
|
|
@ -84,6 +105,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* send mail |
|
|
|
|
* |
|
|
|
|
* @param receivers the receiver list |
|
|
|
|
* @param receiversCc cc list |
|
|
|
|
* @param title the title |
|
|
|
@ -149,6 +171,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* html table content |
|
|
|
|
* |
|
|
|
|
* @param content the content |
|
|
|
|
* @param showAll if show the whole content |
|
|
|
|
* @return the html table form |
|
|
|
@ -159,6 +182,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* html table content |
|
|
|
|
* |
|
|
|
|
* @param content the content |
|
|
|
|
* @return the html table form |
|
|
|
|
*/ |
|
|
|
@ -168,6 +192,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* html text content |
|
|
|
|
* |
|
|
|
|
* @param content the content |
|
|
|
|
* @return text in html form |
|
|
|
|
*/ |
|
|
|
@ -177,9 +202,9 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* send mail as Excel attachment |
|
|
|
|
* |
|
|
|
|
* @param receivers the receiver list |
|
|
|
|
* @param title the title |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
private static void attachment(Collection<String> receivers, Collection<String> receiversCc, String title, String content, String partContent) throws Exception { |
|
|
|
|
MimeMessage msg = getMimeMessage(receivers); |
|
|
|
@ -189,9 +214,9 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get MimeMessage |
|
|
|
|
* |
|
|
|
|
* @param receivers receivers |
|
|
|
|
* @return the MimeMessage |
|
|
|
|
* @throws MessagingException |
|
|
|
|
*/ |
|
|
|
|
private static MimeMessage getMimeMessage(Collection<String> receivers) throws MessagingException { |
|
|
|
|
|
|
|
|
@ -223,7 +248,7 @@ public class MailUtils {
|
|
|
|
|
props.setProperty(Constants.MAIL_SMTP_AUTH, Constants.STRING_TRUE); |
|
|
|
|
props.setProperty(Constants.MAIL_TRANSPORT_PROTOCOL, MAIL_PROTOCOL); |
|
|
|
|
props.setProperty(Constants.MAIL_SMTP_STARTTLS_ENABLE, STARTTLS_ENABLE); |
|
|
|
|
if (SSL_ENABLE) { |
|
|
|
|
if (Boolean.TRUE.equals(SSL_ENABLE)) { |
|
|
|
|
props.setProperty(Constants.MAIL_SMTP_SSL_ENABLE, "true"); |
|
|
|
|
props.setProperty(Constants.MAIL_SMTP_SSL_TRUST, SSL_TRUST); |
|
|
|
|
} |
|
|
|
@ -241,16 +266,15 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* attach content |
|
|
|
|
* |
|
|
|
|
* @param receiversCc the cc list |
|
|
|
|
* @param title the title |
|
|
|
|
* @param content the content |
|
|
|
|
* @param partContent the partContent |
|
|
|
|
* @param msg the message |
|
|
|
|
* @throws MessagingException |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
private static void attachContent(Collection<String> receiversCc, String title, String content, String partContent, MimeMessage msg) throws MessagingException, IOException { |
|
|
|
|
/** |
|
|
|
|
/* |
|
|
|
|
* set receiverCc |
|
|
|
|
*/ |
|
|
|
|
if (CollectionUtils.isNotEmpty(receiversCc)) { |
|
|
|
@ -267,13 +291,13 @@ public class MailUtils {
|
|
|
|
|
part1.setContent(partContent, Constants.TEXT_HTML_CHARSET_UTF_8); |
|
|
|
|
// set attach file
|
|
|
|
|
MimeBodyPart part2 = new MimeBodyPart(); |
|
|
|
|
File file = new File(xlsFilePath + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); |
|
|
|
|
File file = new File(XLS_FILE_PATH + Constants.SINGLE_SLASH + title + Constants.EXCEL_SUFFIX_XLS); |
|
|
|
|
if (!file.getParentFile().exists()) { |
|
|
|
|
file.getParentFile().mkdirs(); |
|
|
|
|
} |
|
|
|
|
// make excel file
|
|
|
|
|
|
|
|
|
|
ExcelUtils.genExcelFile(content,title,xlsFilePath); |
|
|
|
|
ExcelUtils.genExcelFile(content, title, XLS_FILE_PATH); |
|
|
|
|
|
|
|
|
|
part2.attachFile(file); |
|
|
|
|
part2.setFileName(MimeUtility.encodeText(title + Constants.EXCEL_SUFFIX_XLS, Constants.UTF_8, "B")); |
|
|
|
@ -289,21 +313,21 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* the string object map |
|
|
|
|
* |
|
|
|
|
* @param title the title |
|
|
|
|
* @param content the content |
|
|
|
|
* @param showType the showType |
|
|
|
|
* @param retMap the result map |
|
|
|
|
* @param email the email |
|
|
|
|
* @return the result map |
|
|
|
|
* @throws EmailException |
|
|
|
|
*/ |
|
|
|
|
private static Map<String, Object> getStringObjectMap(String title, String content, String showType, Map<String, Object> retMap, HtmlEmail email) throws EmailException { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
/* |
|
|
|
|
* the subject of the message to be sent |
|
|
|
|
*/ |
|
|
|
|
email.setSubject(title); |
|
|
|
|
/** |
|
|
|
|
/* |
|
|
|
|
* to send information, you can use HTML tags in mail content because of the use of HtmlEmail |
|
|
|
|
*/ |
|
|
|
|
if (showType.equals(ShowType.TABLE.getDescp())) { |
|
|
|
@ -322,6 +346,7 @@ public class MailUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* file delete |
|
|
|
|
* |
|
|
|
|
* @param file the file to delete |
|
|
|
|
*/ |
|
|
|
|
public static void deleteFile(File file) { |
|
|
|
@ -336,9 +361,9 @@ public class MailUtils {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* handle exception |
|
|
|
|
* |
|
|
|
|
* @param receivers the receiver list |
|
|
|
|
* @param retMap the result map |
|
|
|
|
* @param e the exception |
|
|
|
@ -347,6 +372,4 @@ public class MailUtils {
|
|
|
|
|
logger.error("Send email to {} failed", receivers, e); |
|
|
|
|
retMap.put(Constants.MESSAGE, "Send email to {" + String.join(",", receivers) + "} failed," + e.toString()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|