| | |
| | | |
| | | import com.whyc.constant.UserOperation; |
| | | import com.whyc.dto.MailDTO; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.pojo.MailQueue; |
| | | import com.whyc.util.ActionUtil; |
| | | import com.whyc.util.MailUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.mail.MessagingException; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | |
| | | @Autowired |
| | | private DocLogService logService; |
| | | |
| | | @Autowired |
| | | private MailQueueService mailQueueService; |
| | | |
| | | @Transactional |
| | | public void sendMail(MailDTO mailDTO) throws MessagingException { |
| | | public Response sendMail(MailDTO mailDTO) { |
| | | Long uId = ActionUtil.getUser().getId(); |
| | | String name=ActionUtil.getUser().getName(); |
| | | mailUtil.sendMailBatch(name,mailDTO.getMailList(),mailDTO.getTitle(),mailDTO.getContent()); |
| | | //日志 |
| | | StringBuilder mailUserStr = new StringBuilder(); |
| | | List<String> mailList = mailDTO.getMailList(); |
| | | for (int i = 0; i < mailList.size(); i++) { |
| | | if(i==0){ |
| | | mailUserStr.append(mailList.get(i)); |
| | | }else{ |
| | | mailUserStr.append(","+mailList.get(i)); |
| | | } |
| | | } |
| | | try { |
| | | mailUtil.sendMailBatch(name,mailDTO.getMailList(),mailDTO.getTitle(),mailDTO.getContent()); |
| | | |
| | | String msgDetail = "发送给邮箱"+mailUserStr+",标题为:"+mailDTO.getTitle()+",内容为:"+mailDTO.getContent(); |
| | | logService.record(uId,name, UserOperation.TYPE_SEND_MAIL.getType(),"邮件发送",msgDetail); |
| | | //日志 |
| | | List<String> mailList = mailDTO.getMailList(); |
| | | for (int i = 0; i < mailList.size(); i++) { |
| | | if(i==0){ |
| | | mailUserStr.append(mailList.get(i)); |
| | | }else{ |
| | | mailUserStr.append(","+mailList.get(i)); |
| | | } |
| | | } |
| | | |
| | | String msgDetail = "发送给邮箱" + mailUserStr + ",标题为:" + mailDTO.getTitle() + ",内容为:" + mailDTO.getContent(); |
| | | logService.record(uId, name, UserOperation.TYPE_SEND_MAIL.getType(), "邮件发送", msgDetail); |
| | | return new Response().set(1,true,"发送完成"); |
| | | } catch (MessagingException ex) { //邮件发送异常,存入数据库待重新发送列表 |
| | | MailQueue queue = new MailQueue(); |
| | | queue.setMails(mailUserStr.toString()); |
| | | queue.setTitle(mailDTO.getTitle()); |
| | | queue.setContent(mailDTO.getContent()); |
| | | queue.setCreateTime(new Date()); |
| | | queue.setSenderUserId(uId); |
| | | queue.setSenderUserName(name); |
| | | mailQueueService.add(queue); |
| | | return new Response().set(1,false,"发送失败,网络异常,网络恢复后自动延时发送"); |
| | | } |
| | | } |
| | | |
| | | public void sendQueueMail(MailQueue queue) { |
| | | Long uId = queue.getSenderUserId(); |
| | | String name = queue.getSenderUserName(); |
| | | |
| | | String mails = queue.getMails(); |
| | | String[] mailSplits = mails.split(","); |
| | | List<String> mailList = new LinkedList<>(Arrays.asList(mailSplits)); |
| | | try{ |
| | | mailUtil.sendMailBatch(name,mailList,queue.getTitle(),queue.getContent()); |
| | | //删除 数据库邮件队列表中 已被发送的邮件记录 |
| | | mailQueueService.deleteById(queue.getId()); |
| | | //日志 |
| | | String msgDetail = "发送给邮箱" + mails + ",标题为:" + queue.getTitle() + ",内容为:" + queue.getContent(); |
| | | logService.record(uId, name, UserOperation.TYPE_SEND_MAIL.getType(), "邮件发送", msgDetail); |
| | | } catch (MessagingException ex) { //邮件发送异常 |
| | | //System.out.println("邮件发送异常,后续会持续发送直到成功"); |
| | | } |
| | | } |
| | | } |