EmailService
package me.zhengjie.tools.service; import me.zhengjie.tools.domain.EmailConfig;
import me.zhengjie.tools.domain.vo.EmailVo;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async; /**
* @author jie
* @date 2018-12-26
*/
@CacheConfig(cacheNames = "email")
public interface EmailService { /**
* 更新邮件配置
* @param emailConfig
* @param old
* @return
*/
@CachePut(key = "'1'")
EmailConfig update(EmailConfig emailConfig, EmailConfig old); /**
* 查询配置
* @return
*/
@Cacheable(key = "'1'")
EmailConfig find(); /**
* 发送邮件
* @param emailVo
* @param emailConfig
* @throws Exception
*/
@Async
void send(EmailVo emailVo, EmailConfig emailConfig) throws Exception;
}
package me.zhengjie.tools.service.impl; import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import me.zhengjie.common.exception.BadRequestException;
import me.zhengjie.common.utils.ElAdminConstant;
import me.zhengjie.core.utils.EncryptUtils;
import me.zhengjie.tools.domain.EmailConfig;
import me.zhengjie.tools.domain.vo.EmailVo;
import me.zhengjie.tools.repository.EmailRepository;
import me.zhengjie.tools.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional; /**
* @author jie
* @date 2018-12-26
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EmailServiceImpl implements EmailService { @Autowired
private EmailRepository emailRepository; @Override
@Transactional(rollbackFor = Exception.class)
public EmailConfig update(EmailConfig emailConfig, EmailConfig old) {
try {
if(!emailConfig.getPass().equals(old.getPass())){
// 对称加密
emailConfig.setPass(EncryptUtils.desEncrypt(emailConfig.getPass()));
}
} catch (Exception e) {
e.printStackTrace();
}
emailRepository.saveAndFlush(emailConfig);
return emailConfig;
} @Override
public EmailConfig find() {
Optional<EmailConfig> emailConfig = emailRepository.findById(1L);
if(emailConfig.isPresent()){
return emailConfig.get();
} else {
return new EmailConfig();
}
} @Override
@Transactional(rollbackFor = Exception.class)
public void send(EmailVo emailVo, EmailConfig emailConfig){
if(emailConfig == null){
throw new BadRequestException("请先配置,再操作");
}
/**
* 封装
*/
MailAccount account = new MailAccount();
account.setHost(emailConfig.getHost());
account.setPort(Integer.parseInt(emailConfig.getPort()));
account.setAuth(true);
try {
// 对称解密
account.setPass(EncryptUtils.desDecrypt(emailConfig.getPass()));
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
}
account.setFrom(emailConfig.getUser()+"<"+emailConfig.getFromUser()+">");
//ssl方式发送
account.setStartttlsEnable(true);
String content = emailVo.getContent()+ ElAdminConstant.EMAIL_CONTENT;
/**
* 发送
*/
try {
MailUtil.send(account,
emailVo.getTos(),
emailVo.getSubject(),
content,
true);
}catch (Exception e){
throw new BadRequestException(e.getMessage());
}
}
}
package me.zhengjie.tools.domain.vo; import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List; /**
* 发送邮件时,接收参数的类
* @author 郑杰
* @date 2018/09/28 12:02:14
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class EmailVo { /**
* 收件人,支持多个收件人,用逗号分隔
*/
@NotEmpty
private List<String> tos; @NotBlank
private String subject; @NotBlank
private String content;
}
package me.zhengjie.tools.domain; import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable; /**
* 邮件配置类,数据存覆盖式存入数据存
* @author jie
* @date 2018-12-26
*/
@Entity
@Data
@Table(name = "email_config")
public class EmailConfig implements Serializable { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; /**
*邮件服务器SMTP地址
*/
@NotBlank
private String host; /**
* 邮件服务器SMTP端口
*/
@NotBlank
private String port; /**
* 发件者用户名,默认为发件人邮箱前缀
*/
@NotBlank
private String user; @NotBlank
private String pass; /**
* 发件人
*/
@NotBlank
private String fromUser;
}
package me.zhengjie.common.utils; /**
* 常用静态常量
* @author jie
* @date 2018-12-26
*/
public class ElAdminConstant { public static final String RESET_PASS = "重置密码"; public static final String RESET_MAIL = "重置邮箱"; public static final String EMAIL_CODE = "<p>你的验证码为:"; public static final String EMAIL_CONTENT = "<p style='text-align: right;'>----- 邮件来自<span style='color: rgb(194, 79, 74);'> <a href='http://auauz.net' target='_blank'>eladmin</a></span> 后台管理系统,系统邮件请勿回复</p>"; /**
* 常用接口
*/
public static class Url{
public static final String SM_MS_URL = "https://sm.ms/api/upload";
}
}
package me.zhengjie.tools.rest; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.common.aop.log.Log;
import me.zhengjie.tools.domain.EmailConfig;
import me.zhengjie.tools.domain.vo.EmailVo;
import me.zhengjie.tools.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; /**
* 发送邮件
* @author 郑杰
* @date 2018/09/28 6:55:53
*/
@Slf4j
@RestController
@RequestMapping("api")
public class EmailController { @Autowired
private EmailService emailService; @GetMapping(value = "/email")
public ResponseEntity get(){
return new ResponseEntity(emailService.find(),HttpStatus.OK);
} @Log(description = "配置邮件")
@PutMapping(value = "/email")
public ResponseEntity emailConfig(@Validated @RequestBody EmailConfig emailConfig){
emailService.update(emailConfig,emailService.find());
return new ResponseEntity(HttpStatus.OK);
} @Log(description = "发送邮件")
@PostMapping(value = "/email")
public ResponseEntity send(@Validated @RequestBody EmailVo emailVo) throws Exception {
log.warn("REST request to send Email : {}" +emailVo);
emailService.send(emailVo,emailService.find());
return new ResponseEntity(HttpStatus.OK);
}
}
package me.zhengjie.tools.repository; import me.zhengjie.tools.domain.EmailConfig;
import org.springframework.data.jpa.repository.JpaRepository; /**
* @author jie
* @date 2018-12-26
*/
public interface EmailRepository extends JpaRepository<EmailConfig,Long> {
}
EmailService的更多相关文章
- Configuring Autofac to work with the ASP.NET Identity Framework in MVC 5
https://developingsoftware.com/configuring-autofac-to-work-with-the-aspnet-identity-framework-in-mvc ...
- .NET单元测试的艺术-2.核心技术
开篇:上一篇我们学习基本的单元测试基础知识和入门实例.但是,如果我们要测试的方法依赖于一个外部资源,如文件系统.数据库.Web服务或者其他难以控制的东西,那又该如何编写测试呢?为了解决这些问题,我们需 ...
- Windows Service--Write a Better Windows Service
原文地址: http://visualstudiomagazine.com/Articles/2005/10/01/Write-a-Better-Windows-Service.aspx?Page=1 ...
- SpringBoot应用部署[转]
在开发spring Boot应用的过程中,Spring Boot直接执行public static void main()函数并启动一个内嵌的应用服务器(取决于类路径上的以来是Tomcat还是jett ...
- 依赖注入 – ASP.NET MVC 4 系列
从 ASP.NET MVC 3.0 开始就引入了一个新概念:依赖解析器(dependence resolver).极大的增强了应用程序参与依赖注入的能力,更好的在 MVC 使用的服务和创 ...
- springMVC发送邮件
springMVC发送邮件 利用javax.mail发送邮件,图片与附件都可发送 1,Controller类 package com.web.controller.api; import javax. ...
- MVC使用ASP.NET Identity 2.0实现用户身份安全相关功能,比如通过短信或邮件发送安全码,账户锁定等
本文体验在MVC中使用ASP.NET Identity 2.0,体验与用户身份安全有关的功能: →install-package Microsoft.AspNet.Identity.Samples - ...
- AspNet Identity and IoC Container Registration
https://github.com/trailmax/IoCIdentitySample TL;DR: Registration code for Autofac, for SimpleInject ...
- spring 部分配置内容备忘
1.spring定时器简单配置: <bean name="taskJob" class="com.netcloud.mail.util.TaskJob"& ...
随机推荐
- css 居中布局方案
position(transform css3 有些浏览器不兼容) <article id="one"> <section id="section&q ...
- Ubuntu系统下QEMU环境搭建
(这篇文章是在搭建QEMU环境时,在网上找到了一些教程资料,并在实际操作中遇到的一些问题的整理) 下载Linux内核 下载内核有两种方法,一种是用git直接下载内核代码树,方便后面的内核开发.另一种是 ...
- h5-全屏插件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL DELETE 语句
可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 可以在 mysql> 命令提示符或 PHP 脚本中执行该命令. 语法 以下是 SQL DELETE 语句从 ...
- 不重复,distinct,row_number() over(partition by)
1.查询不重复的字段 select distinct name from table 2.查询某个字段不重复的,所有内容 sql根据某一个字段重复只取第一条数据 select s.* from ( s ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- 利用salt-stack 对多台分布式应用进行简单部署jar包项目:
/appsystems/JQM-SERVER/shell/stopServer.sh: ----用脚本停止应用 cmd. ...
- RabbitMQ 整合 SpringCloud实战
RabbitMQ 整合 SpringCloud实战RabbitMQ 整合 SpringCloud实战rabbitmq-common 子项目rabbitmq-springcloud-consumer 子 ...
- Go-map-字符串-指针-结构体
Maps 什么是 map ? 类似Python中的字典数据类型,以k:v键值对的形式. map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 如何创建 ma ...
- jq轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...