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