Bladeren bron

feat:批量邮件发送

chenjianhua 1 maand geleden
bovenliggende
commit
1a8b1a3ae6

+ 68 - 62
service-manage/src/main/java/com/simuwang/manage/service/impl/ContactInformationServiceImpl.java

@@ -154,75 +154,81 @@ public class ContactInformationServiceImpl implements ContactInformationService
     }
 
     @Override
-    public ResultVo sendEmail(List<MultipartFile> files, String contractId, String emailTitle, String templateCode, String emailBody,String emailId) {
+    public ResultVo sendEmail(List<MultipartFile> files, String contractIds, String emailTitle, String templateCode, String emailBody,String emailId) {
         ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
-        SendEmailInfoDO sendEmailInfoDO = new SendEmailInfoDO();
-        sendEmailInfoDO.setEmailTitle(emailTitle);
-        sendEmailInfoDO.setTemplateCode(templateCode);
-        sendEmailInfoDO.setEmailBody(emailBody);
-        sendEmailInfoDO.setContractId(Integer.parseInt(contractId));
-        sendEmailInfoDO.setEmailId(Integer.parseInt(emailId));
-        sendEmailInfoDO.setCreateTime(new Date());
-        sendEmailInfoDO.setUpdateTime(new Date());
-        sendEmailInfoDO.setIsvalid(1);
-        sendEmailInfoDO.setCreatorId(UserUtils.getLoginUser().getUserId());
-        sendEmailInfoDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
-        List<File> fileList = new ArrayList<>();
-        if(files != null && !files.isEmpty()){
-            StringBuilder filePath = new StringBuilder();
-            StringBuilder fileName = new StringBuilder();
-            for (MultipartFile file : files) {
-                InputStream inputStream = null;
+        for (String contractId : contractIds.split(",")) {
+            try{
+                SendEmailInfoDO sendEmailInfoDO = new SendEmailInfoDO();
+                sendEmailInfoDO.setEmailTitle(emailTitle);
+                sendEmailInfoDO.setTemplateCode(templateCode);
+                sendEmailInfoDO.setEmailBody(emailBody);
+                sendEmailInfoDO.setContractId(Integer.parseInt(contractId));
+                sendEmailInfoDO.setEmailId(Integer.parseInt(emailId));
+                sendEmailInfoDO.setCreateTime(new Date());
+                sendEmailInfoDO.setUpdateTime(new Date());
+                sendEmailInfoDO.setIsvalid(1);
+                sendEmailInfoDO.setCreatorId(UserUtils.getLoginUser().getUserId());
+                sendEmailInfoDO.setUpdaterId(UserUtils.getLoginUser().getUserId());
+                List<File> fileList = new ArrayList<>();
+                if(files != null && !files.isEmpty()){
+                    StringBuilder filePath = new StringBuilder();
+                    StringBuilder fileName = new StringBuilder();
+                    for (MultipartFile file : files) {
+                        InputStream inputStream = null;
+                        try {
+                            inputStream = file.getInputStream();
+                            String attachmentFilePath = path+"/upload/"+ System.currentTimeMillis()+"/"+file.getOriginalFilename();
+                            File attachmentFile = new File(attachmentFilePath);
+                            FileUtils.copyInputStreamToFile(inputStream,attachmentFile);
+                            inputStream.close();
+                            filePath.append(attachmentFilePath).append(";");
+                            fileName.append(file.getOriginalFilename()).append(";");
+                            fileList.add(attachmentFile);
+                        } catch (IOException e) {
+                            logger.error(e.getMessage(),e);
+                        }
+                    }
+                    sendEmailInfoDO.setFileName(fileName.toString());
+                    sendEmailInfoDO.setFilePath(filePath.toString());
+                }
+                MailboxInfoDO mailboxInfoDO = emailConfigMapper.searchEmailConfigById(Integer.parseInt(emailId));
+                MailboxInfoDTO mailboxInfoDTO = new MailboxInfoDTO();
+                mailboxInfoDTO.setAccount(mailboxInfoDO.getEmail());
+                String password = mailboxInfoDO.getPassword();
+                try{
+                    String publicKey = this.properties.getSecurityRsa().getPublicKey();
+                    String privateKey = this.properties.getSecurityRsa().getPrivateKey();
+                    password = new RSA(privateKey, publicKey).decryptStr(password, KeyType.PrivateKey);
+                }catch (Exception e){
+                    logger.error(e.getMessage(),e);
+                }
+                mailboxInfoDTO.setPassword(password);
+                mailboxInfoDTO.setProtocol(sysConfigMapper.selectConfigByKey("sender.protocol"));
+                mailboxInfoDTO.setPort(sysConfigMapper.selectConfigByKey("sender.port"));
+                mailboxInfoDTO.setHost(sysConfigMapper.selectConfigByKey("sender.host"));
+                mailboxInfoDTO.setSsl(sysConfigMapper.selectConfigByKey("sender.ssl"));
+                ContactInformationDO contactInformationDO = contactInformationMapper.selectById(contractId);
+                vo.setData(true);
                 try {
-                    inputStream = file.getInputStream();
-                    String attachmentFilePath = path+"/upload/"+ System.currentTimeMillis()+"/"+file.getOriginalFilename();
-                    File attachmentFile = new File(attachmentFilePath);
-                    FileUtils.copyInputStreamToFile(inputStream,attachmentFile);
-                    inputStream.close();
-                    filePath.append(attachmentFilePath).append(";");
-                    fileName.append(file.getOriginalFilename()).append(";");
-                    fileList.add(attachmentFile);
-                } catch (IOException e) {
+                    String host = sysConfigMapper.selectConfigByKey("email.host")==null?"":sysConfigMapper.selectConfigByKey("email.host");
+                    emailBody = emailBody.replaceAll("\r\n","<br/>").replaceAll("\n","<br/>");
+                    try{
+                        EmailUtil.senEmail(mailboxInfoDTO,contactInformationDO.getContactEmail(),fileList,emailBody,host,emailTitle);
+                    }catch (Exception e){
+                        logger.error(e.getMessage(),e);
+                        EmailUtil.senEmail(mailboxInfoDTO,contactInformationDO.getContactEmail(),fileList,emailBody,host,emailTitle);
+                    }
+                    sendEmailMapper.insert(sendEmailInfoDO);
+                    saveCompanyEmailSendHistory(contactInformationDO.getCompanyId(),contactInformationDO.getContactEmail(),emailTitle,1, com.simuwang.base.common.enums.ResultCode.SEND_SUCCESS.getMsg());
+                } catch (Exception e) {
                     logger.error(e.getMessage(),e);
+                    vo.setData(false);
+                    vo.setMsg(e.getMessage());
+                    saveCompanyEmailSendHistory(contactInformationDO.getCompanyId(),contactInformationDO.getContactEmail(),emailTitle,0, e.getMessage());
                 }
-            }
-            sendEmailInfoDO.setFileName(fileName.toString());
-            sendEmailInfoDO.setFilePath(filePath.toString());
-        }
-        MailboxInfoDO mailboxInfoDO = emailConfigMapper.searchEmailConfigById(Integer.parseInt(emailId));
-        MailboxInfoDTO mailboxInfoDTO = new MailboxInfoDTO();
-        mailboxInfoDTO.setAccount(mailboxInfoDO.getEmail());
-        String password = mailboxInfoDO.getPassword();
-        try{
-            String publicKey = this.properties.getSecurityRsa().getPublicKey();
-            String privateKey = this.properties.getSecurityRsa().getPrivateKey();
-            password = new RSA(privateKey, publicKey).decryptStr(password, KeyType.PrivateKey);
-        }catch (Exception e){
-            logger.error(e.getMessage(),e);
-        }
-        mailboxInfoDTO.setPassword(password);
-        mailboxInfoDTO.setProtocol(sysConfigMapper.selectConfigByKey("sender.protocol"));
-        mailboxInfoDTO.setPort(sysConfigMapper.selectConfigByKey("sender.port"));
-        mailboxInfoDTO.setHost(sysConfigMapper.selectConfigByKey("sender.host"));
-        mailboxInfoDTO.setSsl(sysConfigMapper.selectConfigByKey("sender.ssl"));
-        ContactInformationDO contactInformationDO = contactInformationMapper.selectById(contractId);
-        vo.setData(true);
-        try {
-            String host = sysConfigMapper.selectConfigByKey("email.host")==null?"":sysConfigMapper.selectConfigByKey("email.host");
-            emailBody = emailBody.replaceAll("\r\n","<br/>").replaceAll("\n","<br/>");
-            try{
-                EmailUtil.senEmail(mailboxInfoDTO,contactInformationDO.getContactEmail(),fileList,emailBody,host,emailTitle);
             }catch (Exception e){
                 logger.error(e.getMessage(),e);
-                EmailUtil.senEmail(mailboxInfoDTO,contactInformationDO.getContactEmail(),fileList,emailBody,host,emailTitle);
             }
-            sendEmailMapper.insert(sendEmailInfoDO);
-            saveCompanyEmailSendHistory(contactInformationDO.getCompanyId(),contactInformationDO.getContactEmail(),emailTitle,1, com.simuwang.base.common.enums.ResultCode.SEND_SUCCESS.getMsg());
-        } catch (Exception e) {
-            logger.error(e.getMessage(),e);
-            vo.setData(false);
-            vo.setMsg(e.getMessage());
-            saveCompanyEmailSendHistory(contactInformationDO.getCompanyId(),contactInformationDO.getContactEmail(),emailTitle,0, e.getMessage());
         }
         return vo;
     }