-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cef022e
commit 8e794c7
Showing
7 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
erha-admin-tools/src/main/java/fun/yizhierha/tools/other/domain/vo/SendEmailVo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
erha-admin-tools/src/main/java/fun/yizhierha/tools/other/service/ToolEmailService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...dmin-tools/src/main/java/fun/yizhierha/tools/other/service/impl/ToolEmailServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |