use of com.tansci.domain.message.TemplateDetails in project tansci by typ1805.
the class AliSmsServiceImpl method send.
/**
* @MonthName: send
* @Description: 发短信
* @Author: tanyp
* @Date: 2021/6/7 14:50
* @Param: [dto]
* @return: MessageVo
*/
@Override
public MessageVo send(MessageDto dto) {
try {
log.info("======发送短信开始,请求参数:{}", JSON.toJSON(dto));
Client client = createClient();
// 组装请求对象
SendSmsRequest request = new SendSmsRequest();
// 外部流水扩展字段
String outId = UUID.randomUUID().toString();
request.setOutId(outId);
// 支持对多个手机号码发送短信,手机号码之间以英文逗号(,)分隔。上限为1000个手机号码。批量调用相对于单条调用及时性稍有延迟。
request.setPhoneNumbers(dto.getPhone());
// 短信签名名称
request.setSignName(smsConfig.getSignName());
// 短信模板ID
request.setTemplateCode(dto.getCode());
// 短信模板变量对应的实际值,JSON格式。如果JSON中需要带换行符,请参照标准的JSON协议处理。
request.setTemplateParam(JSON.toJSONString(dto.getParam()));
// 发送短信
SendSmsResponse res = client.sendSms(request);
TemplateDetails details = new TemplateDetails();
MessageVo message = MessageVo.builder().build();
if (Objects.equals(Constants.NEWS_SUCCESS_CODE, res.body.getCode())) {
log.info("======发送短信成功,返回值:{}", JSON.toJSON(res.body));
message.setCode(Constants.NEWS_SUCCESS_CODE);
message.setMessage(Constants.NEWS_SUCCESS_MESSAGE);
details.setState(0);
} else {
log.info("======发送短信失败,返回值:{}", JSON.toJSON(res.body));
message.setCode(Constants.NEWS_FAIL_CODE);
message.setMessage(Constants.NEWS_FAIL_MESSAGE);
details.setState(1);
}
details.setCode(dto.getCode());
details.setContent(JSON.toJSONString(request));
details.setSendTime(LocalDateTime.now());
templateDetailsService.save(details);
return message;
} catch (Exception e) {
log.error("======发送短信异常:{}", e.getMessage());
e.printStackTrace();
return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
}
}
use of com.tansci.domain.message.TemplateDetails 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();
}
}
use of com.tansci.domain.message.TemplateDetails 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();
}
}
use of com.tansci.domain.message.TemplateDetails 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();
}
}
Aggregations