Ver código fonte

邮件发送配置优化

xuxueli 7 anos atrás
pai
commit
b6f3ad8ab8

+ 22 - 16
xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/MailUtil.java Ver arquivo

@@ -25,14 +25,12 @@ public class MailUtil {
25 25
 	private static String port;
26 26
 	private static String username;
27 27
 	private static String password;
28
-	private static String sendFrom;
29 28
 	private static String sendNick;
30 29
 	static{
31 30
 		host = PropertiesUtil.getString("xxl.job.mail.host");
32 31
 		port = PropertiesUtil.getString("xxl.job.mail.port");
33 32
 		username = PropertiesUtil.getString("xxl.job.mail.username");
34 33
 		password = PropertiesUtil.getString("xxl.job.mail.password");
35
-		sendFrom = PropertiesUtil.getString("xxl.job.mail.sendFrom");
36 34
 		sendNick = PropertiesUtil.getString("xxl.job.mail.sendNick");
37 35
 	}
38 36
 	
@@ -70,8 +68,9 @@ public class MailUtil {
70 68
 		try {
71 69
 			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
72 70
 			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8"); // 设置utf-8或GBK编码,否则邮件会有乱码;multipart,true表示文件上传
73
-			
74
-			helper.setFrom(sendFrom, sendNick);
71
+
72
+
73
+			helper.setFrom(username, sendNick);
75 74
 			helper.setTo(toAddress);
76 75
 
77 76
 			// 设置收件人抄送的名片和地址(相当于群发了)
@@ -111,38 +110,45 @@ public class MailUtil {
111 110
 	public static boolean sendMail (String toAddress, String mailSubject, String mailBody, 
112 111
 			boolean mailBodyIsHtml, File[] attachments){
113 112
         try {
114
-			// 创建邮件发送类 JavaMailSender (用于发送多元化邮件,包括附件,图片,html 等    )
113
+			// 创建邮件发送类 JavaMailSender (用于发送多元化邮件,包括附件,图片,html 等)
115 114
         	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
116 115
         	mailSender.setHost(host); 			// 设置邮件服务主机    
117 116
         	mailSender.setUsername(username); 	// 发送者邮箱的用户名    
118 117
         	mailSender.setPassword(password); 	// 发送者邮箱的密码    
119 118
         	
120
-			//配置文件,用于实例化java.mail.session    
119
+			// 配置文件,用于实例化java.mail.session
121 120
 			Properties pro = new Properties();
121
+			pro.put("mail.transport.protocol", "smtp");
122 122
 			pro.put("mail.smtp.auth", "true");		// 登录SMTP服务器,需要获得授权 (网易163邮箱新近注册的邮箱均不能授权,测试 sohu 的邮箱可以获得授权)
123 123
 			pro.put("mail.smtp.socketFactory.port", port);
124 124
 			pro.put("mail.smtp.socketFactory.fallback", "false");
125 125
 			mailSender.setJavaMailProperties(pro);
126 126
 			
127
-			//创建多元化邮件 (创建 mimeMessage 帮助类,用于封装信息至 mimeMessage)
127
+			// 创建多元化邮件 (创建 mimeMessage 帮助类,用于封装信息至 mimeMessage)
128 128
 			MimeMessage mimeMessage = mailSender.createMimeMessage();						
129 129
 			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8");
130 130
 			
131
-			helper.setFrom(sendFrom, sendNick);
131
+			helper.setFrom(username, sendNick);
132 132
 			helper.setTo(toAddress);
133 133
 
134 134
 			helper.setSubject(mailSubject);
135 135
 			helper.setText(mailBody, mailBodyIsHtml); 
136 136
 			
137
-			// 添加内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
138
-			//helper.addInline(MimeUtility.encodeText(inLineFile.getName()), inLineFile);	
137
+			// 设置收件人抄送的名片和地址(相当于群发)
138
+			//helper.setCc(InternetAddress.parse(MimeUtility.encodeText("邮箱001") + " <@163.com>," + MimeUtility.encodeText("邮箱002") + " <@foxmail.com>"));
139
+
140
+			// 内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
141
+			//helper.addInline(MimeUtility.encodeText(inLineFile.getName()), inLineFile);
139 142
 			
140
-			// 添加附件    
141
-			if (ArrayUtils.isNotEmpty(attachments)) {
143
+			// 添加附件
144
+			/*if (ArrayUtils.isNotEmpty(attachments)) {
142 145
 				for (File file : attachments) {
143 146
 					helper.addAttachment(MimeUtility.encodeText(file.getName()), file);	
144 147
 				}
145
-			}
148
+			}*/
149
+
150
+			// 群发
151
+			//MimeMessage[] mailMessages = { mimeMessage };
146 152
 			
147 153
 			mailSender.send(mimeMessage);
148 154
 			return true;
@@ -156,11 +162,11 @@ public class MailUtil {
156 162
 	public static void main(String[] args) {
157 163
 		
158 164
 		ExecutorService exec = Executors.newCachedThreadPool();
159
-		for (int i = 0; i < 20; i++) {
165
+		for (int i = 0; i < 1; i++) {
160 166
 			exec.execute(new Thread(new Runnable() {
161 167
 				@Override
162 168
 				public void run() {
163
-					while(total < 10){
169
+					while(total < 1){
164 170
 						String mailBody = "<html><head><meta http-equiv="
165 171
 								+ "Content-Type"
166 172
 								+ " content="
@@ -168,7 +174,7 @@ public class MailUtil {
168 174
 								+ "></head><body><h1>新书快递通知</h1>你的新书快递申请已推送新书,请到<a href=''>空间"
169 175
 								+ "</a>中查看</body></html>";
170 176
 						
171
-						sendMail("ovono802302@163.com", "测试邮件", mailBody, false, null);
177
+						sendMail("931591021@qq.com", "测试邮件", mailBody, true, null);
172 178
 						System.out.println(total);
173 179
 						total++;
174 180
 					}

+ 0 - 1
xxl-job-admin/src/main/resources/xxl-job-admin.properties Ver arquivo

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