邮件发送 FIXME部分需要详细说明一下

This commit is contained in:
wangjingyi 2022-03-26 07:50:58 +08:00
parent 132c8b0e84
commit bd86b89ea5
5 changed files with 76 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccou
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailSendVO;
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import cn.iocoder.yudao.module.system.service.mail.MailAccountService; import cn.iocoder.yudao.module.system.service.mail.MailAccountService;
@ -81,4 +82,11 @@ public class MailAccountController {
list.sort(Comparator.comparing(MailAccountDO::getId)); list.sort(Comparator.comparing(MailAccountDO::getId));
return success(MailAccountConvert.INSTANCE.convertList02(list)); return success(MailAccountConvert.INSTANCE.convertList02(list));
} }
@PostMapping("/send")
@ApiOperation("发送邮件")
@PreAuthorize("@ss.hasPermission('system:mail-account:send')")
public CommonResult<Boolean> sendMail(MailSendVO mailSendVO){
mailAccountService.sendMail(mailSendVO);
return success(true);
}
} }

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.system.controller.admin.mail.vo.send;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class MailSendVO {
@ApiModelProperty(value = "邮箱" , required = true , example = "yudaoyuanma@123.com")
private String from;
@ApiModelProperty(value = "标题" , example = "标题")
private String title;
@ApiModelProperty(value = "内容" , example = "内容")
private String content;
@ApiModelProperty(value = "收件人" , required = true , example = "yudaoyuanma@123.com")
private List<String> tos;
@ApiModelProperty(value = "附件" , example = "附件编码")
private List<String> fileIds;
}

View File

@ -16,7 +16,7 @@ import lombok.experimental.Accessors;
@Data @Data
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@Accessors(chain = true) @Accessors(chain = true)
@ApiModel(value="MailAccount对象", description="") @ApiModel(value="MailAccount对象", description="邮箱账号")
@TableName(value = "system_mail_account", autoResultMap = true) @TableName(value = "system_mail_account", autoResultMap = true)
public class MailAccountDO extends BaseDO implements Serializable { public class MailAccountDO extends BaseDO implements Serializable {
@ -37,7 +37,7 @@ public class MailAccountDO extends BaseDO implements Serializable {
private String host; private String host;
@TableField("port") @TableField("port")
private String port; private Integer port;
@TableField("sslEnable") @TableField("sslEnable")
private Boolean sslEnable; private Boolean sslEnable;

View File

@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailSendVO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import java.util.List; import java.util.List;
@ -30,4 +31,6 @@ public interface MailAccountService {
PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO); PageResult<MailAccountDO> getMailAccountPage(MailAccountPageReqVO pageReqVO);
List<MailAccountDO> getMailAccountList(); List<MailAccountDO> getMailAccountList();
void sendMail(MailSendVO mailSendVO);
} }

View File

@ -1,12 +1,17 @@
package cn.iocoder.yudao.module.system.service.mail.impl; package cn.iocoder.yudao.module.system.service.mail.impl;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailSendVO;
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper;
import cn.iocoder.yudao.module.system.service.mail.MailAccountService; import cn.iocoder.yudao.module.system.service.mail.MailAccountService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -35,6 +40,9 @@ public class MailAccountServiceImpl implements MailAccountService {
@Resource @Resource
private MailAccountMapper mailAccountMapper; private MailAccountMapper mailAccountMapper;
@Resource
private MailTemplateMapper mailTemplateMapper;
@Override @Override
public Long create(MailAccountCreateReqVO createReqVO) { public Long create(MailAccountCreateReqVO createReqVO) {
// username 要校验唯一 // username 要校验唯一
@ -81,6 +89,35 @@ public class MailAccountServiceImpl implements MailAccountService {
return mailAccountMapper.selectList(); return mailAccountMapper.selectList();
} }
@Override
public void sendMail(MailSendVO mailSendVO) {
// FIXME 查询模版信息 查询模版多条时 使用规则是什么
List<MailTemplateDO> mailTemplateDOList = mailTemplateMapper.selectList(
"username",mailSendVO.getFrom()
);
//查询账号信息
MailAccountDO mailAccountDO = mailAccountMapper.selectOne(
"from",mailSendVO.getFrom()
);
// FIXME 模版和邮件内容合成方式未知
String content = mailSendVO.getContent();
String templateContent = "";
// 后续功能 TODO 附件查询
//List<String> fileIds = mailSendVO.getFileIds();
//装载账号信息
MailAccount account = new MailAccount();
account.setHost(mailAccountDO.getHost());
account.setPort(mailAccountDO.getPort());
account.setAuth(true);
account.setFrom(mailAccountDO.getFrom());
account.setUser(mailAccountDO.getUsername());
account.setPass(mailAccountDO.getPassword());
account.setSslEnable(mailAccountDO.getSslEnable());
//发送
MailUtil.send(account , mailSendVO.getTos() , mailSendVO.getTitle() , mailSendVO.getContent() , false);
}
private void validateMailAccountExists(Long id) { private void validateMailAccountExists(Long id) {
if (mailAccountMapper.selectById(id) == null) { if (mailAccountMapper.selectById(id) == null) {
throw exception(MAIL_ACCOUNT_NOT_EXISTS); throw exception(MAIL_ACCOUNT_NOT_EXISTS);