提交 | 用户 | age
|
f51ff9
|
1 |
package com.yc.crm.mail.action; |
X |
2 |
|
877259
|
3 |
import com.sun.mail.imap.IMAPStore; |
X |
4 |
import com.yc.action.mail.EmailConfigUtils; |
3b74e3
|
5 |
import com.yc.crm.mail.entity.FoundationEntity; |
385e2d
|
6 |
import com.yc.crm.mail.entity.T482102Entity; |
X |
7 |
import com.yc.crm.mail.service.MailAccountIfc; |
|
8 |
import com.yc.crm.mail.service.MailIfc; |
3b74e3
|
9 |
import com.yc.crm.mail.service.MailServiceIfc; |
f51ff9
|
10 |
import com.yc.crm.mail.util.AllBackMsg; |
X |
11 |
import com.yc.entity.DataSourceEntity; |
3b74e3
|
12 |
import com.yc.factory.FactoryBean; |
f51ff9
|
13 |
import com.yc.multiData.MultiDataSource; |
X |
14 |
import com.yc.multiData.SpObserver; |
|
15 |
import com.yc.utils.SessionKey; |
|
16 |
import org.apache.commons.lang3.StringUtils; |
|
17 |
import org.springframework.beans.factory.annotation.Autowired; |
3b74e3
|
18 |
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
f51ff9
|
19 |
import org.springframework.web.bind.annotation.*; |
X |
20 |
|
428443
|
21 |
import javax.mail.*; |
f51ff9
|
22 |
import javax.servlet.http.HttpServletRequest; |
X |
23 |
import javax.servlet.http.HttpServletResponse; |
|
24 |
import javax.servlet.http.HttpSession; |
428443
|
25 |
import java.util.HashMap; |
f51ff9
|
26 |
import java.util.List; |
428443
|
27 |
import java.util.Map; |
X |
28 |
import java.util.Properties; |
f51ff9
|
29 |
|
X |
30 |
/** |
|
31 |
* @BelongsProject: eCoWorksV3 |
|
32 |
* @BelongsPackage: com.yc.crm.action |
|
33 |
* @author: xinyb |
|
34 |
* @CreateTime: 2024-07-19 17:33 |
385e2d
|
35 |
* @Description: 个人邮箱配置 |
f51ff9
|
36 |
*/ |
X |
37 |
@CrossOrigin |
|
38 |
@RestController |
385e2d
|
39 |
@RequestMapping("/crm/mail/account") |
X |
40 |
public class MailAccount { |
3b74e3
|
41 |
ThreadPoolTaskExecutor threadPoolExecutor = (ThreadPoolTaskExecutor) FactoryBean.getBean("threadPoolExecutor");//线程池 |
f51ff9
|
42 |
|
X |
43 |
@Autowired |
385e2d
|
44 |
MailAccountIfc mailAccountIfc; |
X |
45 |
@Autowired |
|
46 |
MailIfc mailIfc; |
3b74e3
|
47 |
@Autowired |
X |
48 |
MailServiceIfc mailServiceIfc; |
f51ff9
|
49 |
/** |
X |
50 |
* 添加邮箱配置信息 |
|
51 |
* |
|
52 |
* @param account |
|
53 |
* @param request |
|
54 |
* @param response |
|
55 |
* @return |
|
56 |
*/ |
385e2d
|
57 |
@PostMapping("/addAccount.do") |
X |
58 |
public AllBackMsg addAccount(@RequestBody T482102Entity account, HttpServletRequest request, HttpServletResponse response) { |
f51ff9
|
59 |
AllBackMsg msg = new AllBackMsg(); |
X |
60 |
try { |
|
61 |
if (StringUtils.isBlank(account.getEmail())) { |
8c1986
|
62 |
msg.setFail("请输入邮箱账号"); |
X |
63 |
return msg; |
|
64 |
} |
877259
|
65 |
if (account.getMailType().equals(0) || account.getMailType().equals(1)) { |
8c1986
|
66 |
if (StringUtils.isBlank(account.getPassword())) { |
X |
67 |
msg.setFail("请输入邮箱密码"); |
|
68 |
return msg; |
|
69 |
} |
513b8e
|
70 |
account.setReceiveEmail(account.getEmail()); |
X |
71 |
account.setReceivePassword(account.getPassword()); |
|
72 |
account.setSmtpEmail(account.getEmail()); |
|
73 |
account.setSmtpPassword(account.getPassword()); |
877259
|
74 |
} |
X |
75 |
if (account.getMailType().equals(1) || account.getMailType().equals(2)) { |
|
76 |
if (StringUtils.isBlank(account.getReceiveProtocol())) { |
|
77 |
msg.setFail("请选择协议类型"); |
8c1986
|
78 |
return msg; |
X |
79 |
} |
877259
|
80 |
if (StringUtils.isBlank(account.getReceiveHost())) { |
X |
81 |
msg.setFail("请输入收邮件服务器"); |
8c1986
|
82 |
return msg; |
X |
83 |
} |
877259
|
84 |
if (account.getReceivePort().equals(0)) { |
X |
85 |
msg.setFail("请输入收邮件服务器端口"); |
8c1986
|
86 |
return msg; |
X |
87 |
} |
877259
|
88 |
if (StringUtils.isBlank(account.getSmtpHost())) { |
X |
89 |
msg.setFail("请输入发邮件服务器"); |
8c1986
|
90 |
return msg; |
X |
91 |
} |
877259
|
92 |
if (account.getSmtpPort().equals(0)) { |
X |
93 |
msg.setFail("请输入发邮件服务器端口"); |
|
94 |
return msg; |
|
95 |
} |
|
96 |
if (account.getMailType().equals(2)) { |
|
97 |
if (StringUtils.isBlank(account.getReceiveEmail())) { |
|
98 |
msg.setFail("请输入收件账号"); |
|
99 |
return msg; |
|
100 |
} |
|
101 |
if (StringUtils.isBlank(account.getReceivePassword())) { |
|
102 |
msg.setFail("请输入收件密码"); |
|
103 |
return msg; |
|
104 |
} |
|
105 |
if (StringUtils.isBlank(account.getSmtpEmail())) { |
|
106 |
msg.setFail("请输入发件账号"); |
|
107 |
return msg; |
|
108 |
} |
|
109 |
if (StringUtils.isBlank(account.getSmtpPassword())) { |
|
110 |
msg.setFail("请输入发件密码"); |
|
111 |
return msg; |
|
112 |
} |
|
113 |
} |
f51ff9
|
114 |
} |
X |
115 |
HttpSession session = request.getSession(); |
|
116 |
String companyId = (String) session.getAttribute(SessionKey.COMPANY_ID); |
|
117 |
String companyName = (String) session.getAttribute(SessionKey.COMPANY_NAME); |
|
118 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
119 |
String userName = (String) session.getAttribute(SessionKey.USER_NAME); |
|
120 |
account.setCompanyId(companyId); |
|
121 |
account.setCompanyName(companyName); |
|
122 |
account.setUserCode(userCode); |
|
123 |
account.setUserName(userName); |
877259
|
124 |
if (account.getMailType().equals(0)) { |
X |
125 |
//初始配置 |
|
126 |
getMailConfigProperties(account); |
|
127 |
} |
f51ff9
|
128 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
X |
129 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
877259
|
130 |
//邮件检测 |
X |
131 |
String error = emailDetection(account); |
|
132 |
if (StringUtils.isNotBlank(error)) { |
|
133 |
account.setMailStatus("异常"); |
|
134 |
} |
3b74e3
|
135 |
account= mailAccountIfc.addEmailAccount(account);//添加新的邮箱配置 |
428443
|
136 |
Map<String, Object> map = new HashMap<>(); |
877259
|
137 |
if (StringUtils.isBlank(error)) { |
7caeae
|
138 |
map.put("code", 0); |
877259
|
139 |
map.put("companyId", companyId); |
X |
140 |
map.put("companyName", companyName); |
|
141 |
map.put("userCode", userCode); |
|
142 |
map.put("userName", userName); |
|
143 |
map.put("email", account.getEmail()); |
|
144 |
msg.setSuccess("已保存", map); |
3b74e3
|
145 |
//多线程收邮件 |
X |
146 |
threadPoolExecutor.execute(new MailController.ReceivingEmailsSave(mailServiceIfc, account, new FoundationEntity(request))); |
877259
|
147 |
} else { |
7caeae
|
148 |
map.put("code", -1); |
877259
|
149 |
map.put("mailStatus", "异常"); |
X |
150 |
map.put("error", error); |
|
151 |
msg.setSuccess("执行成功", map); |
|
152 |
} |
|
153 |
} catch (Exception e) { |
|
154 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
155 |
} finally { |
|
156 |
SpObserver.setDBtoInstance(); |
|
157 |
} |
|
158 |
return msg; |
|
159 |
} |
|
160 |
|
|
161 |
/** |
|
162 |
* 添加别名邮箱 |
|
163 |
* |
|
164 |
* @param account |
|
165 |
* @param request |
|
166 |
* @param response |
|
167 |
* @return |
|
168 |
*/ |
|
169 |
@GetMapping("/addAliasEmail.do") |
|
170 |
public AllBackMsg addAliasEmail(String aliasEmail, Integer accountId, HttpServletRequest request, HttpServletResponse response) { |
|
171 |
AllBackMsg msg = new AllBackMsg(); |
|
172 |
try { |
|
173 |
if (StringUtils.isBlank(aliasEmail)) { |
|
174 |
msg.setFail("请输入别名邮箱"); |
|
175 |
return msg; |
|
176 |
} |
|
177 |
if (accountId.equals(0)) { |
|
178 |
msg.setFail("邮箱账号Id不能为空"); |
|
179 |
return msg; |
|
180 |
} |
|
181 |
HttpSession session = request.getSession(); |
|
182 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
183 |
if (StringUtils.isBlank(userCode)) { |
|
184 |
msg.setFail("登录用户已过期,请检查"); |
|
185 |
return msg; |
|
186 |
} |
|
187 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
188 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
189 |
T482102Entity t482102Entity = mailAccountIfc.getAccountInfo(userCode, accountId); |
|
190 |
if (t482102Entity.getMailType().equals(0) || t482102Entity.getMailType().equals(1)) { |
|
191 |
t482102Entity.setEmail(aliasEmail); |
|
192 |
t482102Entity.setReceiveEmail(aliasEmail); |
|
193 |
t482102Entity.setSmtpEmail(aliasEmail); |
|
194 |
} |
|
195 |
//邮箱检测 |
|
196 |
String error = emailDetection(t482102Entity); |
|
197 |
Map<String, Object> map = new HashMap<>(); |
|
198 |
if (StringUtils.isBlank(error)) { |
|
199 |
mailAccountIfc.updateAliasEmail(aliasEmail, accountId); |
7caeae
|
200 |
map.put("code", 0); |
877259
|
201 |
map.put("userCode", userCode); |
X |
202 |
map.put("aliasEmail", aliasEmail); |
|
203 |
map.put("accountId", accountId); |
|
204 |
msg.setSuccess("已保存", map); |
|
205 |
} else { |
7caeae
|
206 |
map.put("code", -1); |
877259
|
207 |
map.put("mailStatus", "异常"); |
X |
208 |
map.put("error", error); |
|
209 |
msg.setSuccess("执行成功", map); |
|
210 |
} |
f51ff9
|
211 |
} catch (Exception e) { |
f5cc47
|
212 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
f51ff9
|
213 |
} finally { |
X |
214 |
SpObserver.setDBtoInstance(); |
|
215 |
} |
|
216 |
return msg; |
|
217 |
} |
|
218 |
|
|
219 |
/** |
|
220 |
* 更新邮箱配置信息 |
|
221 |
* |
|
222 |
* @param account |
|
223 |
* @param request |
|
224 |
* @param response |
|
225 |
* @return |
|
226 |
*/ |
385e2d
|
227 |
@PostMapping("/updateAccount.do") |
3c2c1b
|
228 |
public AllBackMsg updateAccount(@RequestBody T482102Entity account, HttpServletRequest request, HttpServletResponse response) { |
f51ff9
|
229 |
AllBackMsg msg = new AllBackMsg(); |
X |
230 |
try { |
428443
|
231 |
if (account.getAccountId() == 0) { |
f5cc47
|
232 |
msg.setFail("获取不到邮箱配置ID,请检查"); |
X |
233 |
return msg; |
|
234 |
} |
8c1986
|
235 |
if (StringUtils.isBlank(account.getEmail())) { |
X |
236 |
msg.setFail("请输入邮箱账号"); |
|
237 |
return msg; |
|
238 |
} |
|
239 |
if (account.getMailType().equals(0)) { |
877259
|
240 |
account.setMailType(1); |
X |
241 |
} |
|
242 |
if (account.getMailType().equals(1)) { |
8c1986
|
243 |
if (StringUtils.isBlank(account.getPassword())) { |
X |
244 |
msg.setFail("请输入邮箱密码"); |
|
245 |
return msg; |
|
246 |
} |
|
247 |
account.setReceiveEmail(account.getEmail()); |
|
248 |
account.setReceivePassword(account.getPassword()); |
|
249 |
account.setSmtpEmail(account.getEmail()); |
|
250 |
account.setSmtpPassword(account.getPassword()); |
877259
|
251 |
} else if (account.getMailType().equals(2)) { |
8c1986
|
252 |
if (StringUtils.isBlank(account.getReceiveEmail())) { |
X |
253 |
msg.setFail("请输入收件账号"); |
|
254 |
return msg; |
|
255 |
} |
|
256 |
if (StringUtils.isBlank(account.getReceivePassword())) { |
|
257 |
msg.setFail("请输入收件密码"); |
|
258 |
return msg; |
|
259 |
} |
|
260 |
if (StringUtils.isBlank(account.getSmtpEmail())) { |
|
261 |
msg.setFail("请输入发件账号"); |
|
262 |
return msg; |
|
263 |
} |
|
264 |
if (StringUtils.isBlank(account.getSmtpPassword())) { |
|
265 |
msg.setFail("请输入发件密码"); |
|
266 |
return msg; |
|
267 |
} |
|
268 |
} |
|
269 |
if (StringUtils.isBlank(account.getReceiveHost())) { |
|
270 |
msg.setFail("请输入收邮件服务器"); |
|
271 |
return msg; |
|
272 |
} |
|
273 |
if (account.getReceivePort().equals(0)) { |
|
274 |
msg.setFail("请输入收邮件服务器端口"); |
|
275 |
return msg; |
|
276 |
} |
|
277 |
if (StringUtils.isBlank(account.getSmtpHost())) { |
|
278 |
msg.setFail("请输入发邮件服务器"); |
|
279 |
return msg; |
|
280 |
} |
|
281 |
if (account.getSmtpPort().equals(0)) { |
|
282 |
msg.setFail("请输入发邮件服务器端口"); |
|
283 |
return msg; |
|
284 |
} |
f51ff9
|
285 |
HttpSession session = request.getSession(); |
f5cc47
|
286 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
428443
|
287 |
String userName = (String) session.getAttribute(SessionKey.USER_NAME); |
X |
288 |
if (StringUtils.isBlank(userCode)) { |
f5cc47
|
289 |
msg.setFail("登录用户已过期,请检查"); |
X |
290 |
return msg; |
|
291 |
} |
|
292 |
account.setUserCode(userCode); |
f51ff9
|
293 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
X |
294 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
385e2d
|
295 |
mailAccountIfc.updateEmailAccount(account); |
877259
|
296 |
String error = emailDetection(account); |
428443
|
297 |
Map<String, Object> map = new HashMap<>(); |
877259
|
298 |
if (StringUtils.isBlank(error)) { |
7caeae
|
299 |
map.put("code", 0); |
877259
|
300 |
map.put("userCode", userCode); |
X |
301 |
map.put("userName", userName); |
|
302 |
map.put("email", account.getEmail()); |
|
303 |
msg.setSuccess("已更新", map); |
|
304 |
} else { |
|
305 |
//异常处理 |
|
306 |
mailAccountIfc.updateMailStatus("异常", account.getAccountId()); |
7caeae
|
307 |
map.put("code", -1); |
877259
|
308 |
map.put("mailStatus", "异常"); |
X |
309 |
map.put("error", error); |
|
310 |
msg.setSuccess("执行成功", map); |
|
311 |
} |
f51ff9
|
312 |
} catch (Exception e) { |
f5cc47
|
313 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
385e2d
|
314 |
} finally { |
X |
315 |
SpObserver.setDBtoInstance(); |
f51ff9
|
316 |
} |
X |
317 |
return msg; |
|
318 |
} |
|
319 |
|
|
320 |
/** |
|
321 |
* 删除邮箱配置信息 |
|
322 |
* |
|
323 |
* @param account |
|
324 |
* @param request |
|
325 |
* @param response |
|
326 |
* @return |
|
327 |
*/ |
385e2d
|
328 |
@PostMapping("/deleteAccount.do") |
X |
329 |
public AllBackMsg deleteAccount(@RequestParam(defaultValue = "0") Integer accountId, HttpServletRequest request, HttpServletResponse response) { |
f51ff9
|
330 |
AllBackMsg msg = new AllBackMsg(); |
X |
331 |
try { |
ee2316
|
332 |
if (accountId.equals(0)) { |
385e2d
|
333 |
msg.setFail("邮箱编号ID获取不到"); |
X |
334 |
return msg; |
|
335 |
} |
|
336 |
HttpSession session = request.getSession(); |
|
337 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
428443
|
338 |
if (StringUtils.isBlank(userCode)) { |
f5cc47
|
339 |
msg.setFail("登录用户已过期,请检查"); |
X |
340 |
return msg; |
|
341 |
} |
385e2d
|
342 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
X |
343 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
344 |
Integer cont = mailAccountIfc.deleteEmailAccount(userCode, accountId); |
|
345 |
if (cont > 0) { |
f5cc47
|
346 |
msg.setOk("已删除"); |
385e2d
|
347 |
} |
f51ff9
|
348 |
} catch (Exception e) { |
f5cc47
|
349 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
385e2d
|
350 |
} finally { |
X |
351 |
SpObserver.setDBtoInstance(); |
f51ff9
|
352 |
} |
385e2d
|
353 |
return msg; |
f51ff9
|
354 |
} |
X |
355 |
|
|
356 |
/** |
877259
|
357 |
* 查询邮箱配置列表 |
f51ff9
|
358 |
* |
385e2d
|
359 |
* @param search 搜索 |
f51ff9
|
360 |
* @param request |
X |
361 |
* @param response |
|
362 |
* @return |
|
363 |
*/ |
6a63e2
|
364 |
@GetMapping("/getAccountList.do") |
X |
365 |
public AllBackMsg getAccountList(HttpServletRequest request, HttpServletResponse response) { |
f51ff9
|
366 |
AllBackMsg msg = new AllBackMsg(); |
X |
367 |
try { |
|
368 |
HttpSession session = request.getSession(); |
385e2d
|
369 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
X |
370 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
371 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
877259
|
372 |
List<T482102Entity> t482102Entities = mailAccountIfc.getAccount(userCode); |
385e2d
|
373 |
if (t482102Entities.size() > 0) { |
6a63e2
|
374 |
msg.setSuccess("执行成功", t482102Entities); |
428443
|
375 |
} else { |
f5cc47
|
376 |
msg.setFail("未找到配置信息,0条数据"); |
6a63e2
|
377 |
} |
X |
378 |
} catch (Exception e) { |
|
379 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
380 |
} finally { |
|
381 |
SpObserver.setDBtoInstance(); |
|
382 |
} |
|
383 |
return msg; |
|
384 |
} |
|
385 |
|
|
386 |
|
|
387 |
/** |
|
388 |
* 查询个人邮箱信息 |
|
389 |
* |
|
390 |
* @param search 搜索 |
|
391 |
* @param request |
|
392 |
* @param response |
|
393 |
* @return |
|
394 |
*/ |
|
395 |
@GetMapping("/getAccount.do") |
|
396 |
public AllBackMsg getAccount(String mail, HttpServletRequest request, HttpServletResponse response) { |
|
397 |
AllBackMsg msg = new AllBackMsg(); |
|
398 |
try { |
|
399 |
if (StringUtils.isBlank(mail)) { |
|
400 |
msg.setFail("参数 邮件账号 不能为空"); |
|
401 |
return msg; |
|
402 |
} |
|
403 |
HttpSession session = request.getSession(); |
|
404 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
405 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
406 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
407 |
T482102Entity t482102Entities = mailAccountIfc.getAccountInfo(userCode, mail); |
|
408 |
if (StringUtils.isNotBlank(t482102Entities.getEmail())) { |
|
409 |
msg.setSuccess("执行成功", t482102Entities); |
|
410 |
} else { |
|
411 |
msg.setFail("未找到配置信息"); |
f51ff9
|
412 |
} |
X |
413 |
} catch (Exception e) { |
f5cc47
|
414 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
f51ff9
|
415 |
} finally { |
X |
416 |
SpObserver.setDBtoInstance(); |
|
417 |
} |
385e2d
|
418 |
return msg; |
f51ff9
|
419 |
} |
428443
|
420 |
|
X |
421 |
/** |
f632e7
|
422 |
* 检测绑定是否已经存在(是否已绑定) |
428443
|
423 |
* |
X |
424 |
* @param email |
|
425 |
* @param request |
|
426 |
* @param response |
|
427 |
* @return |
|
428 |
*/ |
53241c
|
429 |
@GetMapping("/eEmailExists.do") |
X |
430 |
public AllBackMsg eEmailExists(String email, HttpServletRequest request, HttpServletResponse response) { |
|
431 |
AllBackMsg msg = new AllBackMsg(); |
|
432 |
|
|
433 |
try { |
|
434 |
HttpSession session = request.getSession(); |
|
435 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
436 |
if (StringUtils.isBlank(userCode)) { |
|
437 |
msg.setFail("登录用户已过期,请检查"); |
|
438 |
return msg; |
|
439 |
} |
|
440 |
if (StringUtils.isBlank(email)) { |
|
441 |
msg.setFail("邮箱不能为空"); |
|
442 |
return msg; |
|
443 |
} |
|
444 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
445 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
446 |
//根据当前用户查询绑定的邮箱信息 |
|
447 |
T482102Entity emailEntity = mailAccountIfc.getAccountInfo(userCode, email);//返回邮箱的账号信息 |
|
448 |
Map<String, Object> map = new HashMap<>(); |
|
449 |
if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) { |
|
450 |
map.put("code", 0); |
|
451 |
map.put("msg", "该邮箱未绑定"); |
|
452 |
} else { |
|
453 |
map.put("code", -1); |
|
454 |
map.put("msg", "该邮箱已绑定"); |
|
455 |
} |
|
456 |
msg.setSuccess("执行成功", map); |
|
457 |
} catch (MessagingException e) { |
|
458 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
459 |
} catch (Exception e) { |
|
460 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
461 |
} finally { |
|
462 |
SpObserver.setDBtoInstance(); |
|
463 |
} |
|
464 |
return msg; |
|
465 |
} |
|
466 |
|
|
467 |
/** |
|
468 |
* 检测绑定是否有效 |
|
469 |
* |
|
470 |
* @param email |
|
471 |
* @param request |
|
472 |
* @param response |
|
473 |
* @return |
|
474 |
*/ |
428443
|
475 |
@GetMapping("/isEmailValid.do") |
X |
476 |
public AllBackMsg isEmailValid(String email, HttpServletRequest request, HttpServletResponse response) { |
|
477 |
AllBackMsg msg = new AllBackMsg(); |
877259
|
478 |
|
428443
|
479 |
try { |
X |
480 |
HttpSession session = request.getSession(); |
|
481 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
482 |
if (StringUtils.isBlank(userCode)) { |
|
483 |
msg.setFail("登录用户已过期,请检查"); |
|
484 |
return msg; |
|
485 |
} |
|
486 |
if (StringUtils.isBlank(email)) { |
|
487 |
msg.setFail("检测绑定邮箱不能为空"); |
|
488 |
return msg; |
|
489 |
} |
|
490 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
491 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
492 |
//根据当前用户查询绑定的邮箱信息 |
|
493 |
T482102Entity emailEntity = mailAccountIfc.getAccountInfo(userCode, email);//返回邮箱的账号信息 |
|
494 |
if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) { |
|
495 |
msg.setFail("找不到邮箱:" + email + "配置信息请完善。"); |
|
496 |
return msg; |
|
497 |
} |
877259
|
498 |
String error = emailDetection(emailEntity); |
6a63e2
|
499 |
Map<String, Object> map = new HashMap<>(); |
877259
|
500 |
if (StringUtils.isBlank(error)) { |
7caeae
|
501 |
map.put("code", 0); |
877259
|
502 |
map.put("email", emailEntity.getEmail()); |
X |
503 |
map.put("protocol", emailEntity.getReceiveProtocol()); |
|
504 |
map.put("receiveHost", emailEntity.getReceiveHost()); |
|
505 |
map.put("receiveSSL", emailEntity.isReceiveSSL()); |
|
506 |
map.put("receivePort", emailEntity.getReceivePort()); |
|
507 |
map.put("smtpHost", emailEntity.getSmtpHost()); |
|
508 |
map.put("smtpSSL", emailEntity.isSmtpSSL()); |
|
509 |
map.put("smtpPort", emailEntity.getSmtpPort()); |
|
510 |
map.put("proxyFlag", emailEntity.isProxyFlag()); |
|
511 |
map.put("biSyncFlag", emailEntity.isBiSyncFlag()); |
7cf738
|
512 |
map.put("mailStatus", "正常"); |
877259
|
513 |
msg.setSuccess("执行成功", map); |
X |
514 |
} else { |
|
515 |
//异常处理 |
|
516 |
mailAccountIfc.updateMailStatus("异常", emailEntity.getAccountId()); |
7caeae
|
517 |
map.put("code", -1); |
877259
|
518 |
map.put("mailStatus", "异常"); |
X |
519 |
map.put("error", error); |
|
520 |
msg.setSuccess("执行成功", map); |
|
521 |
} |
428443
|
522 |
} catch (MessagingException e) { |
X |
523 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
524 |
} catch (Exception e) { |
|
525 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
526 |
} finally { |
|
527 |
SpObserver.setDBtoInstance(); |
|
528 |
} |
|
529 |
return msg; |
|
530 |
} |
877259
|
531 |
|
X |
532 |
/** |
|
533 |
* 邮箱检测 |
|
534 |
* |
|
535 |
* @param emailEntity |
|
536 |
* @return |
|
537 |
* @throws MessagingException |
|
538 |
*/ |
|
539 |
private String emailDetection(T482102Entity emailEntity) throws MessagingException { |
|
540 |
String errorMsg = ""; |
|
541 |
try { |
048b74
|
542 |
String protocol = emailEntity.getReceiveProtocol().toLowerCase();//接收协议 imap pop3 |
877259
|
543 |
Properties props = new Properties(); |
048b74
|
544 |
props.put("mail.store.protocol", protocol);//指定接收的邮件协议 IMAP over pop3 |
X |
545 |
if (protocol.contains("imap")) { |
|
546 |
props.put("mail.imaps.host", emailEntity.getReceiveHost()); |
|
547 |
props.put("mail.imaps.port", emailEntity.getReceivePort()); |
|
548 |
} else if (protocol.contains("pop3")) { |
|
549 |
props.put("mail.pop3.host", emailEntity.getReceiveHost()); |
|
550 |
props.put("mail.pop3.port", emailEntity.getReceivePort()); |
|
551 |
} else { |
|
552 |
props.put("mail.imaps.host", emailEntity.getReceiveHost()); |
|
553 |
props.put("mail.imaps.port", emailEntity.getReceivePort()); |
|
554 |
} |
|
555 |
// props.put("mail.imap.ssl.enable", emailEntity.isReceiveSSL()+""); // 使用 SSL 连接 |
|
556 |
// props.put("mail.imap.starttls.enable", "true");//// IMAP 协议设置 STARTTLS |
877259
|
557 |
|
048b74
|
558 |
props.put("mail.smtp.auth", "true"); |
X |
559 |
props.put("mail.smtp.ssl.enable", emailEntity.isSmtpSSL() + "");//启用SSL |
877259
|
560 |
props.put("mail.smtp.host", emailEntity.getSmtpHost()); // 替换为你的SMTP服务器 |
X |
561 |
props.put("mail.smtp.port", emailEntity.getSmtpPort()); // 或者你使用的端口 |
048b74
|
562 |
|
X |
563 |
// props.put("mail.smtp.starttls.enable", "true");//启用tls |
|
564 |
// props.setProperty("mail.smtp.socketFactory.port", "587");//启用tls后port:587 |
|
565 |
|
877259
|
566 |
|
X |
567 |
HashMap IAM = new HashMap(); |
|
568 |
//带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。 |
|
569 |
IAM.put("name", emailEntity.getReceiveEmail()); |
|
570 |
IAM.put("version", emailEntity.getDocVersion() + ""); |
|
571 |
IAM.put("vendor", emailEntity.getCompanyName()); |
|
572 |
IAM.put("support-email", emailEntity.getEmail()); |
|
573 |
|
|
574 |
//创建会话 |
|
575 |
Session session = Session.getInstance(props, new Authenticator() { |
|
576 |
@Override |
|
577 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
578 |
return new PasswordAuthentication(emailEntity.getReceiveEmail(), emailEntity.getReceivePassword()); |
|
579 |
} |
|
580 |
}); |
|
581 |
|
|
582 |
//存储对象 |
048b74
|
583 |
IMAPStore store = (IMAPStore) session.getStore(protocol);//imap协议或pop3协议类型(推荐你使用IMAP协议来存取服务器上的邮件。) |
877259
|
584 |
//连接 |
X |
585 |
store.connect(emailEntity.getReceiveHost(), emailEntity.getReceiveEmail(), emailEntity.getReceivePassword()); |
|
586 |
store.id(IAM);//163邮箱需要,不然会报:A3 NO SELECT Unsafe Login. Please contact kefu@188.com for help |
|
587 |
Folder folder = null; |
|
588 |
// 获得收件箱 |
|
589 |
folder = store.getFolder("INBOX"); |
|
590 |
// 以读写模式打开收件箱 |
|
591 |
folder.open(Folder.READ_WRITE); |
|
592 |
folder.close(false); |
|
593 |
store.close(); |
|
594 |
|
|
595 |
Transport transport = session.getTransport(); |
|
596 |
transport.connect(); |
|
597 |
transport.close(); |
7cf738
|
598 |
mailAccountIfc.updateMailStatus("正常", emailEntity.getAccountId()); |
877259
|
599 |
} catch (MessagingException e) { |
X |
600 |
errorMsg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage(); |
|
601 |
} catch (Exception e) { |
|
602 |
errorMsg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage(); |
|
603 |
} |
|
604 |
return errorMsg; |
|
605 |
} |
|
606 |
|
f632e7
|
607 |
/** |
X |
608 |
* 常规查询 |
|
609 |
* @param request |
|
610 |
* @param response |
|
611 |
* @return |
|
612 |
*/ |
|
613 |
@GetMapping("/getConvention.do") |
|
614 |
public AllBackMsg getConvention(HttpServletRequest request, HttpServletResponse response) { |
|
615 |
AllBackMsg msg = new AllBackMsg(); |
|
616 |
|
|
617 |
try { |
|
618 |
HttpSession session = request.getSession(); |
|
619 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
620 |
if (StringUtils.isBlank(userCode)) { |
|
621 |
msg.setFail("登录用户已过期,请检查"); |
|
622 |
return msg; |
|
623 |
} |
|
624 |
|
|
625 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
626 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
627 |
//根据当前用户查询绑定的邮箱信息 |
|
628 |
|
|
629 |
} catch (MessagingException e) { |
|
630 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
631 |
} catch (Exception e) { |
|
632 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
633 |
} finally { |
|
634 |
SpObserver.setDBtoInstance(); |
|
635 |
} |
|
636 |
return msg; |
|
637 |
} |
|
638 |
|
|
639 |
/** |
|
640 |
* 常规更新 |
|
641 |
* @param request |
|
642 |
* @param response |
|
643 |
* @return |
|
644 |
*/ |
|
645 |
@GetMapping("/updateConvention.do") |
|
646 |
public AllBackMsg updateConvention(HttpServletRequest request, HttpServletResponse response) { |
|
647 |
AllBackMsg msg = new AllBackMsg(); |
|
648 |
try { |
|
649 |
HttpSession session = request.getSession(); |
|
650 |
String userCode = (String) session.getAttribute(SessionKey.USERCODE); |
|
651 |
if (StringUtils.isBlank(userCode)) { |
|
652 |
msg.setFail("登录用户已过期,请检查"); |
|
653 |
return msg; |
|
654 |
} |
|
655 |
|
|
656 |
DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源 |
|
657 |
SpObserver.setDBtoInstance("_" + dataSource.getDbId()); |
|
658 |
//根据当前用户查询绑定的邮箱信息 |
|
659 |
|
|
660 |
} catch (MessagingException e) { |
|
661 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
662 |
} catch (Exception e) { |
|
663 |
msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); |
|
664 |
} finally { |
|
665 |
SpObserver.setDBtoInstance(); |
|
666 |
} |
|
667 |
return msg; |
|
668 |
} |
|
669 |
|
877259
|
670 |
private T482102Entity getMailConfigProperties(T482102Entity t482102) { |
X |
671 |
try { |
|
672 |
String email = t482102.getEmail(); |
|
673 |
int index = email.lastIndexOf("@"); |
|
674 |
String emailSuffix = email.substring(index + 1).toLowerCase();//邮箱后缀 |
|
675 |
String port = ""; |
|
676 |
t482102.setReceiveHost(EmailConfigUtils.getMailConfig("mail.imap.host." + emailSuffix)); |
|
677 |
t482102.setSmtpHost(EmailConfigUtils.getMailConfig("mail.smtp.host." + emailSuffix)); |
|
678 |
port = EmailConfigUtils.getMailConfig("mail.imap.port." + emailSuffix); |
|
679 |
if (StringUtils.isNotBlank(port)) { |
|
680 |
t482102.setReceivePort(Integer.parseInt(port)); |
|
681 |
} |
|
682 |
port = EmailConfigUtils.getMailConfig("mail.smtp.port." + emailSuffix); |
|
683 |
if (StringUtils.isNotBlank(port)) { |
|
684 |
t482102.setSmtpPort(Integer.parseInt(port)); |
|
685 |
} |
|
686 |
} catch (Exception e) { |
|
687 |
return t482102; |
|
688 |
} |
|
689 |
return t482102; |
|
690 |
} |
f51ff9
|
691 |
} |