Skip to content

Commit

Permalink
feat: 完善[系统工具:邮件工具]功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Forever2ha committed Jan 18, 2023
1 parent cef022e commit 8e794c7
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
5 changes: 5 additions & 0 deletions erha-admin-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
</dependency>-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import fun.yizhierha.common.utils.*;
import fun.yizhierha.tools.other.domain.ToolEmailConfig;
import fun.yizhierha.tools.other.domain.vo.CreateToolEmailConfigVo;
import fun.yizhierha.tools.other.domain.vo.SendEmailVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolEmailConfigVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolEmailConfigVo;

import fun.yizhierha.tools.other.service.ToolEmailConfigService;
import fun.yizhierha.tools.other.service.ToolEmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -34,6 +36,7 @@
public class ToolEmailController{

private final ToolEmailConfigService toolEmailConfigService;
private final ToolEmailService toolEmailService;

@ApiOperation("获取邮件配置")
@Log("获取邮件配置")
Expand Down Expand Up @@ -101,4 +104,22 @@ public void download(HttpServletResponse response){
toolEmailConfigService.download(response);
}

@ApiOperation("发送邮件")
@Log("发送邮件")
@PostMapping
@PreAuthorize("@eh.check('tools:email:post')")
public R sendEmail(@RequestBody @Validated SendEmailVo sendEmailVo){
toolEmailService.sendEmail(sendEmailVo);
return R.ok();
}

@ApiOperation("激活邮件配置")
@Log("激活邮件配置")
@PostMapping("/config/active/{configId}")
@PreAuthorize("@eh.check('tools:email:config:edit')")
public R active(@PathVariable Long configId){
toolEmailConfigService.active(configId);
return R.ok();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package fun.yizhierha.tools.other.domain.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;

@Data
@ApiModel("发送邮件vo")
public class SendEmailVo {

@Email(message = "邮件地址不合法")
@ApiModelProperty("发送地址")
private String address;

@NotBlank(message = "发送内容不能为空")
@ApiModelProperty("内容")
private String content;

@NotBlank(message = "主题不能为空")
@ApiModelProperty("主题")
private String title;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public interface ToolEmailConfigService extends IService<ToolEmailConfig>{

void download(HttpServletResponse response);

ToolEmailConfig getActive();

void active(Long configId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fun.yizhierha.tools.other.service;

import fun.yizhierha.tools.other.domain.vo.SendEmailVo;

public interface ToolEmailService {
void sendEmail(SendEmailVo sendEmailVo);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fun.yizhierha.tools.other.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.exception.InternalServerException;
import fun.yizhierha.common.utils.PageUtils;
import fun.yizhierha.common.utils.Query;
import fun.yizhierha.common.utils.ValidList;
Expand Down Expand Up @@ -86,4 +88,31 @@ public void download(HttpServletResponse response) {
ExcelUtils.export(response,"邮件配置信息表",this.list(), ToolEmailConfig.class);
}

@Override
public ToolEmailConfig getActive() {
ToolEmailConfig one = null;
try {
one = this.getOne(new QueryWrapper<ToolEmailConfig>().eq(ToolEmailConfig.COL_ACTIVE, true));
}catch (Exception e){
throw new InternalServerException("激活的邮件配置不止一个,请核查业务逻辑");
}

return one;
}

@Transactional(rollbackFor = Exception.class)
@Override
public synchronized void active(Long configId) {
if (configId == null) throw new BadRequestException("configId不能为空");
// 将其他激活配置改为未激活
this.update(new UpdateWrapper<ToolEmailConfig>()
.eq(ToolEmailConfig.COL_ACTIVE,true)
.set(ToolEmailConfig.COL_ACTIVE,false));
// 激活当前配置
this.update(new UpdateWrapper<ToolEmailConfig>()
.eq(ToolEmailConfig.COL_CONFIG_ID,configId)
.set(ToolEmailConfig.COL_ACTIVE,true)
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package fun.yizhierha.tools.other.service.impl;

import cn.hutool.extra.mail.Mail;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import fun.yizhierha.common.exception.BadRequestException;
import fun.yizhierha.tools.other.domain.ToolEmailConfig;
import fun.yizhierha.tools.other.domain.vo.SendEmailVo;
import fun.yizhierha.tools.other.service.ToolEmailConfigService;
import fun.yizhierha.tools.other.service.ToolEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ToolEmailServiceImpl implements ToolEmailService {

@Autowired
private ToolEmailConfigService toolEmailConfigService;

@Override
public void sendEmail(SendEmailVo sendEmailVo) {
// 查询激活的配置
ToolEmailConfig toolEmailConfig = toolEmailConfigService.getActive();
if (toolEmailConfig == null)throw new BadRequestException("不存在激活的邮件配置");

MailAccount account = getMainAccount(toolEmailConfig);
// 发送
try {
Mail.create(account)
.setTos(sendEmailVo.getAddress())
.setTitle(sendEmailVo.getTitle())
.setContent(sendEmailVo.getContent(),true)
// 关闭session
.setUseGlobalSession(false)
.send();
}catch (Exception e){
throw new BadRequestException(e.getMessage());
}
}

private MailAccount getMainAccount(ToolEmailConfig cfg) {
MailAccount account = new MailAccount();
account.setHost(cfg.getHost());
account.setPort(Integer.valueOf(cfg.getPort()));
account.setAuth(true);
account.setFrom(cfg.getUser()+"<"+cfg.getFromUser()+">");
account.setUser(cfg.getFromUser().split("@")[0]);
account.setPass(cfg.getPass());
// ssl
account.setSslEnable(true);
account.setStarttlsEnable(true);
return account;
}
}

0 comments on commit 8e794c7

Please sign in to comment.