Search in sources :

Example 1 with Template

use of com.tansci.domain.message.Template in project tansci by typ1805.

the class MailServiceImpl method sendSimple.

/**
 * @MonthName: sendSimple
 * @Description: 普通邮件发送
 * @Author: tanyp
 * @Date: 2021/6/7 9:30
 * @Param: [dto]
 * @return: void
 */
@Override
public MessageVo sendSimple(MessageDto dto) {
    try {
        log.info("=======普通邮件发送开始,请求参数:{}", JSON.toJSON(dto));
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject(dto.getSubject());
        // 设置邮件发送者,这个跟application.yml中设置的要一致
        message.setFrom(sender);
        // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(dto.getRecipient());
        // 设置邮件抄送人,可以有多个抄送人
        if (Objects.nonNull(dto.getCc())) {
            message.setCc(dto.getCc());
        }
        // 设置隐秘抄送人,可以有多个
        if (Objects.nonNull(dto.getBcc())) {
            message.setBcc(dto.getBcc());
        }
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
        if (Objects.isNull(template)) {
            throw new BusinessException("邮件模板编码不存在,请核实!");
        }
        dto.setText(template.getCode());
        String text = MessageUtils.assembly(dto.getText(), dto.getParam());
        if (Objects.isNull(text)) {
            throw new BusinessException("邮件模板参数有误,请核查!");
        }
        message.setText(text);
        // 发送邮件
        javaMailSender.send(message);
        log.info("=======普通邮件发送结束");
        TemplateDetails details = new TemplateDetails();
        details.setCode(dto.getCode());
        details.setState(0);
        details.setContent(JSON.toJSONString(message));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
    } catch (MailException e) {
        log.error("====邮件====sendSimple=====异常:{}", e);
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) MailException(org.springframework.mail.MailException) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 2 with Template

use of com.tansci.domain.message.Template in project tansci by typ1805.

the class MailServiceImpl method sendImgRes.

/**
 * @MonthName: sendImgRes
 * @Description: 带图片资源的邮件
 * @Author: tanyp
 * @Date: 2021/6/7 9:30
 * @Param: [dto]
 * @return: void
 */
@Override
public MessageVo sendImgRes(MessageDto dto) {
    try {
        log.info("=======带图片资源的邮件开始,请求参数:{}", JSON.toJSON(dto));
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setSubject(dto.getSubject());
        message.setFrom(sender);
        message.setTo(dto.getRecipient());
        if (Objects.nonNull(dto.getCc())) {
            message.setCc(dto.getCc());
        }
        if (Objects.nonNull(dto.getBcc())) {
            message.setBcc(dto.getBcc());
        }
        message.setSentDate(new Date());
        Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
        if (Objects.isNull(template)) {
            throw new BusinessException("邮件模板编码不存在,请核实!");
        }
        dto.setText(template.getContent());
        String text = MessageUtils.assembly(dto.getText(), dto.getParam());
        if (Objects.isNull(text)) {
            throw new BusinessException("邮件模板参数有误,请核查!");
        }
        message.setText(text);
        // 第一个参数指的是html中占位符的名字,第二个参数就是文件的位置
        dto.getAttachments().forEach(file -> {
            try {
                message.addInline(file.getName(), new FileSystemResource(file));
            } catch (MessagingException e) {
                log.error("=========邮件图片解析异常:{}", e);
            }
        });
        javaMailSender.send(mimeMessage);
        log.info("=======带图片资源的邮件结束");
        TemplateDetails details = new TemplateDetails();
        details.setCode(dto.getCode());
        details.setState(0);
        details.setContent(JSON.toJSONString(message));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
    } catch (MessagingException e) {
        log.error("====邮件====sendImgRes=====异常:{}", e);
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) FileSystemResource(org.springframework.core.io.FileSystemResource) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 3 with Template

use of com.tansci.domain.message.Template in project tansci by typ1805.

the class MailServiceImpl method sendAttachFile.

/**
 * @MonthName: sendAttachFile
 * @Description: 带附件的邮件
 * @Author: tanyp
 * @Date: 2021/6/7 9:30
 * @Param: [dto]
 * @return: void
 */
@Override
public MessageVo sendAttachFile(MessageDto dto) {
    try {
        log.info("=======带附件的邮件开始,请求参数:{}", JSON.toJSON(dto));
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // true表示构建一个可以带附件的邮件对象
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
        message.setSubject(dto.getSubject());
        message.setFrom(sender);
        message.setTo(dto.getRecipient());
        if (Objects.nonNull(dto.getCc())) {
            message.setCc(dto.getCc());
        }
        if (Objects.nonNull(dto.getBcc())) {
            message.setBcc(dto.getBcc());
        }
        message.setSentDate(new Date());
        Template template = templateService.getOne(Wrappers.<Template>lambdaQuery().eq(Template::getCode, dto.getCode()));
        if (Objects.isNull(template)) {
            throw new BusinessException("邮件模板编码不存在,请核实!");
        }
        dto.setText(template.getContent());
        String text = MessageUtils.assembly(dto.getText(), dto.getParam());
        if (Objects.isNull(text)) {
            throw new BusinessException("邮件模板参数有误,请核查!");
        }
        message.setText(text);
        // 第一个参数是自定义的名称,后缀需要加上,第二个参数是文件的位置
        dto.getAttachments().forEach(file -> {
            try {
                message.addAttachment(file.getName(), file);
            } catch (MessagingException e) {
                log.error("=========邮件附件解析异常:{}", e);
            }
        });
        javaMailSender.send(mimeMessage);
        log.info("=======带附件的邮件结束");
        TemplateDetails details = new TemplateDetails();
        details.setCode(dto.getCode());
        details.setState(0);
        details.setContent(JSON.toJSONString(message));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return MessageVo.builder().code(Constants.NEWS_SUCCESS_CODE).message(Constants.NEWS_SUCCESS_MESSAGE).build();
    } catch (MessagingException e) {
        log.error("==========邮件====sendAttachFile=====异常:{}", e);
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) TemplateDetails(com.tansci.domain.message.TemplateDetails) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date) Template(com.tansci.domain.message.Template)

Example 4 with Template

use of com.tansci.domain.message.Template in project tansci by typ1805.

the class TemplateServiceImpl method saveTemplate.

@Transactional
@Override
public Integer saveTemplate(Template template) {
    log.info("=========saveTemplate:{}", JSON.toJSON(template));
    String id = UUIDUtils.getUUID();
    template.setId(id);
    template.setDelFlag(0);
    template.setCreater(SecurityUserUtils.getUser().getId());
    template.setUpdateTime(LocalDateTime.now());
    template.setCreateTime(LocalDateTime.now());
    int row = 0;
    switch(template.getBusinessType()) {
        case 0:
            // 短信
            template.setState(0);
            row = this.baseMapper.insert(template);
            if (row > 0) {
                // 请求阿里云短信API,添加模板
                MessageVo message = aliSmsServiceImpl.addSmsTemplate(SmsTemplateDto.builder().templateType(template.getType()).templateName(template.getName()).templateContent(template.getContent()).remark(template.getRemark()).build());
                // 更新模板
                Template tem = new Template();
                tem.setId(id);
                if (message.getCode().equals(Constants.NEWS_SUCCESS_CODE)) {
                    tem.setCode(message.getTemplateCode());
                    tem.setState(0);
                } else {
                    tem.setState(2);
                }
                tem.setUpdateTime(LocalDateTime.now());
                this.updateById(tem);
            }
            break;
        case 1:
            // 邮件
            template.setState(1);
            template.setCode("YJ" + System.currentTimeMillis());
            row = this.baseMapper.insert(template);
            break;
        default:
            throw new BusinessException("添加失败,业务类型不存在!");
    }
    return row;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) MessageVo(com.tansci.domain.message.vo.MessageVo) Template(com.tansci.domain.message.Template) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Template

use of com.tansci.domain.message.Template in project tansci by typ1805.

the class TemplateServiceImpl method delTemplate.

@Transient
@Override
public Integer delTemplate(String id) {
    log.info("=========delTemplate:{}", id);
    Template template = this.baseMapper.selectById(id);
    if (Objects.isNull(template)) {
        throw new BusinessException("删除失败,模板不存在!");
    }
    int row = 0;
    switch(template.getBusinessType()) {
        case 0:
            // 请求阿里云短信API,修改模板
            MessageVo message = aliSmsServiceImpl.deleteSmsTemplate(SmsTemplateDto.builder().templateCode(template.getCode()).build());
            // 更新模板
            if (!message.getCode().equals(Constants.NEWS_SUCCESS_CODE)) {
                template.setDelFlag(1);
                template.setUpdateTime(LocalDateTime.now());
                this.baseMapper.updateById(template);
            }
            break;
        case 1:
            // 邮件
            template.setDelFlag(1);
            row = this.baseMapper.updateById(template);
            break;
        default:
            throw new BusinessException("删除失败,业务类型不存在!");
    }
    return row;
}
Also used : BusinessException(com.tansci.common.exception.BusinessException) MessageVo(com.tansci.domain.message.vo.MessageVo) Template(com.tansci.domain.message.Template) Transient(java.beans.Transient)

Aggregations

BusinessException (com.tansci.common.exception.BusinessException)5 Template (com.tansci.domain.message.Template)5 TemplateDetails (com.tansci.domain.message.TemplateDetails)3 Date (java.util.Date)3 MessageVo (com.tansci.domain.message.vo.MessageVo)2 MessagingException (javax.mail.MessagingException)2 MimeMessage (javax.mail.internet.MimeMessage)2 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)2 Transient (java.beans.Transient)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 MailException (org.springframework.mail.MailException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1 Transactional (org.springframework.transaction.annotation.Transactional)1