|
@@ -1,19 +1,16 @@
|
|
|
package com.xxl.job.admin.core.util;
|
|
|
|
|
|
-import org.apache.commons.lang.ArrayUtils;
|
|
|
+import org.apache.commons.mail.DefaultAuthenticator;
|
|
|
+import org.apache.commons.mail.EmailException;
|
|
|
+import org.apache.commons.mail.HtmlEmail;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.mail.javamail.JavaMailSenderImpl;
|
|
|
-import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
|
|
|
-import javax.mail.internet.MimeMessage;
|
|
|
-import java.io.File;
|
|
|
-import java.util.Properties;
|
|
|
-import java.util.concurrent.ExecutorService;
|
|
|
-import java.util.concurrent.Executors;
|
|
|
+import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
|
* 邮件发送.Util
|
|
|
+ *
|
|
|
* @author xuxueli 2016-3-12 15:06:20
|
|
|
*/
|
|
|
public class MailUtil {
|
|
@@ -33,89 +30,42 @@ public class MailUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
- * 发送邮件 (完整版) (纯JavaMail)
|
|
|
- *
|
|
|
- * @param toAddress : 收件人邮箱
|
|
|
- * @param mailSubject : 邮件主题
|
|
|
- * @param mailBody : 邮件正文
|
|
|
- * @param mailBodyIsHtml: 邮件正文格式,true:HTML格式;false:文本格式
|
|
|
- *
|
|
|
- * @param attachments : 附件
|
|
|
+ *
|
|
|
+ * @param toAddress 收件人邮箱
|
|
|
+ * @param mailSubject 邮件主题
|
|
|
+ * @param mailBody 邮件正文
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public static boolean sendMail (String toAddress, String mailSubject, String mailBody,
|
|
|
- boolean mailBodyIsHtml, File[] attachments){
|
|
|
- try {
|
|
|
-
|
|
|
- JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
|
|
- mailSender.setHost(host);
|
|
|
- mailSender.setUsername(username);
|
|
|
- mailSender.setPassword(password);
|
|
|
-
|
|
|
-
|
|
|
- Properties pro = new Properties();
|
|
|
- pro.put("mail.transport.protocol", "smtp");
|
|
|
- pro.put("mail.smtp.auth", "true");
|
|
|
- pro.put("mail.smtp.socketFactory.port", port);
|
|
|
- pro.put("mail.smtp.socketFactory.fallback", "false");
|
|
|
- mailSender.setJavaMailProperties(pro);
|
|
|
-
|
|
|
-
|
|
|
- MimeMessage mimeMessage = mailSender.createMimeMessage();
|
|
|
- MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8");
|
|
|
-
|
|
|
- helper.setFrom(username, sendNick);
|
|
|
- helper.setTo(toAddress);
|
|
|
+ public static boolean sendMail(String toAddress, String mailSubject, String mailBody){
|
|
|
|
|
|
- helper.setSubject(mailSubject);
|
|
|
- helper.setText(mailBody, mailBodyIsHtml);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ try {
|
|
|
+
|
|
|
+ HtmlEmail email = new HtmlEmail();
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- for (File file : attachments) {
|
|
|
- helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
|
|
|
- }
|
|
|
- }*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- mailSender.send(mimeMessage);
|
|
|
+ email.setHostName(host);
|
|
|
+ email.setSmtpPort(Integer.valueOf(port));
|
|
|
+
|
|
|
+ email.setAuthenticator(new DefaultAuthenticator(username, password));
|
|
|
+ email.setCharset(Charset.defaultCharset().name());
|
|
|
+
|
|
|
+ email.setFrom(username, sendNick);
|
|
|
+ email.addTo(toAddress);
|
|
|
+ email.setSubject(mailSubject);
|
|
|
+ email.setMsg(mailBody);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ email.send();
|
|
|
return true;
|
|
|
- } catch (Exception e) {
|
|
|
+ } catch (EmailException e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
+
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
- static int total = 0;
|
|
|
- public static void main(String[] args) {
|
|
|
-
|
|
|
- ExecutorService exec = Executors.newCachedThreadPool();
|
|
|
- for (int i = 0; i < 1; i++) {
|
|
|
- exec.execute(new Thread(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- while(total < 1){
|
|
|
- String mailBody = "<html><head><meta http-equiv="
|
|
|
- + "Content-Type"
|
|
|
- + " content="
|
|
|
- + "text/html; charset=gb2312"
|
|
|
- + "></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间"
|
|
|
- + "</a>中查看</body></html>";
|
|
|
-
|
|
|
- sendMail("931591021@qq.com", "测试邮件", mailBody, true, null);
|
|
|
- System.out.println(total);
|
|
|
- total++;
|
|
|
- }
|
|
|
- }
|
|
|
- }));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
}
|