邮件发送配置优化

pull/4/head
xuxueli 7 years ago
parent 7b639a14b1
commit b6f3ad8ab8

@ -25,14 +25,12 @@ public class MailUtil {
private static String port; private static String port;
private static String username; private static String username;
private static String password; private static String password;
private static String sendFrom;
private static String sendNick; private static String sendNick;
static{ static{
host = PropertiesUtil.getString("xxl.job.mail.host"); host = PropertiesUtil.getString("xxl.job.mail.host");
port = PropertiesUtil.getString("xxl.job.mail.port"); port = PropertiesUtil.getString("xxl.job.mail.port");
username = PropertiesUtil.getString("xxl.job.mail.username"); username = PropertiesUtil.getString("xxl.job.mail.username");
password = PropertiesUtil.getString("xxl.job.mail.password"); password = PropertiesUtil.getString("xxl.job.mail.password");
sendFrom = PropertiesUtil.getString("xxl.job.mail.sendFrom");
sendNick = PropertiesUtil.getString("xxl.job.mail.sendNick"); sendNick = PropertiesUtil.getString("xxl.job.mail.sendNick");
} }
@ -71,7 +69,8 @@ public class MailUtil {
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 设置utf-8或GBK编码否则邮件会有乱码;multipart,true表示文件上传 MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 设置utf-8或GBK编码否则邮件会有乱码;multipart,true表示文件上传
helper.setFrom(sendFrom, sendNick);
helper.setFrom(username, sendNick);
helper.setTo(toAddress); helper.setTo(toAddress);
// 设置收件人抄送的名片和地址(相当于群发了) // 设置收件人抄送的名片和地址(相当于群发了)
@ -119,6 +118,7 @@ public class MailUtil {
// 配置文件用于实例化java.mail.session // 配置文件用于实例化java.mail.session
Properties pro = new Properties(); Properties pro = new Properties();
pro.put("mail.transport.protocol", "smtp");
pro.put("mail.smtp.auth", "true"); // 登录SMTP服务器,需要获得授权 (网易163邮箱新近注册的邮箱均不能授权,测试 sohu 的邮箱可以获得授权) pro.put("mail.smtp.auth", "true"); // 登录SMTP服务器,需要获得授权 (网易163邮箱新近注册的邮箱均不能授权,测试 sohu 的邮箱可以获得授权)
pro.put("mail.smtp.socketFactory.port", port); pro.put("mail.smtp.socketFactory.port", port);
pro.put("mail.smtp.socketFactory.fallback", "false"); pro.put("mail.smtp.socketFactory.fallback", "false");
@ -128,21 +128,27 @@ public class MailUtil {
MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8");
helper.setFrom(sendFrom, sendNick); helper.setFrom(username, sendNick);
helper.setTo(toAddress); helper.setTo(toAddress);
helper.setSubject(mailSubject); helper.setSubject(mailSubject);
helper.setText(mailBody, mailBodyIsHtml); helper.setText(mailBody, mailBodyIsHtml);
// 添加内嵌文件第1个参数为cid标识这个文件,第2个参数为资源 // 设置收件人抄送的名片和地址(相当于群发)
//helper.setCc(InternetAddress.parse(MimeUtility.encodeText("邮箱001") + " <@163.com>," + MimeUtility.encodeText("邮箱002") + " <@foxmail.com>"));
// 内嵌文件第1个参数为cid标识这个文件,第2个参数为资源
//helper.addInline(MimeUtility.encodeText(inLineFile.getName()), inLineFile); //helper.addInline(MimeUtility.encodeText(inLineFile.getName()), inLineFile);
// 添加附件 // 添加附件
if (ArrayUtils.isNotEmpty(attachments)) { /*if (ArrayUtils.isNotEmpty(attachments)) {
for (File file : attachments) { for (File file : attachments) {
helper.addAttachment(MimeUtility.encodeText(file.getName()), file); helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
} }
} }*/
// 群发
//MimeMessage[] mailMessages = { mimeMessage };
mailSender.send(mimeMessage); mailSender.send(mimeMessage);
return true; return true;
@ -156,11 +162,11 @@ public class MailUtil {
public static void main(String[] args) { public static void main(String[] args) {
ExecutorService exec = Executors.newCachedThreadPool(); ExecutorService exec = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 1; i++) {
exec.execute(new Thread(new Runnable() { exec.execute(new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
while(total < 10){ while(total < 1){
String mailBody = "<html><head><meta http-equiv=" String mailBody = "<html><head><meta http-equiv="
+ "Content-Type" + "Content-Type"
+ " content=" + " content="
@ -168,7 +174,7 @@ public class MailUtil {
+ "></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间" + "></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间"
+ "</a>中查看</body></html>"; + "</a>中查看</body></html>";
sendMail("ovono802302@163.com", "测试邮件", mailBody, false, null); sendMail("931591021@qq.com", "测试邮件", mailBody, true, null);
System.out.println(total); System.out.println(total);
total++; total++;
} }

@ -9,7 +9,6 @@ xxl.job.mail.host=smtp.163.com
xxl.job.mail.port=25 xxl.job.mail.port=25
xxl.job.mail.username=ovono802302@163.com xxl.job.mail.username=ovono802302@163.com
xxl.job.mail.password=asdfzxcv xxl.job.mail.password=asdfzxcv
xxl.job.mail.sendFrom=ovono802302@163.com
xxl.job.mail.sendNick=《任务调度平台XXL-JOB》 xxl.job.mail.sendNick=《任务调度平台XXL-JOB》
### xxl-job login ### xxl-job login

Loading…
Cancel
Save