目录
在跑个人应用的时候,想引入一个通知机制,在应用启动和停止的时候通知下自己(因为应用部署在服务器上,不想每次都到服务器上看);
- 发送短信要申请模板,而且还收费...
- 发送手机通知又太多依赖;
- 钉钉、微信;至于钉钉和微信骚扰消息太多了,容易漏掉通知信息
剩下的就是发送邮箱、免费、而且没有那么多的通知干扰,邮箱App在收到邮件的时候会自动的发生一条Push。而且配置起来很简单... 那就动手操作起来吧。
配置发送邮件
1、引入SpringBoot的Mail依赖
如果是Spring Boot应用不需要指定版本号了
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2、配置邮箱发送的Bean
如下配置
在配置前获取QQ邮箱授权码;QQ邮件在Spring的Bean中密码为授权码。
@Configuration public class MailConfig { @Bean public JavaMailSender getJavaMailSender() { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); // mailSender.setHost("smtp.gmail.com"); mailSender.setHost("smtp.qq.com"); mailSender.setPort(587); mailSender.setUsername("aihehe123@foxmail.com"); mailSender.setPassword("这里是QQ邮箱的授权码"); Properties props = mailSender.getJavaMailProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.debug", "true"); return mailSender; } }
3、发送测试邮件
发送普通文本
SimpleMailMessage simpleMessage = new SimpleMailMessage(); simpleMessage.setFrom("aihehe123@foxmail.com"); simpleMessage.setTo("aihehe123@foxmail.com"); simpleMessage.setSubject("test"); simpleMessage.setText("hello"); javaMailSender.send(simpleMessage);
发送带附件的文本
MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true); mimeMessageHelper.setFrom("aihehe123@foxmail.com"); mimeMessageHelper.setTo("aihehe123@foxmail.com"); mimeMessageHelper.setSubject("test"); mimeMessageHelper.setText("hello"); String path = "http://gif.aihe.space/dd467677ea35485da3d12326fcf36e5d.gif"; UrlResource fileUrlResource = new UrlResource(path); mimeMessageHelper.addAttachment(UrlUtils.parseUrlName(path), fileUrlResource); javaMailSender.send(message);
4、查看效果
前两个邮件是带附件的,最下面一个是不带附件的;
应用启动&停止邮件通知
1、配置发送邮件的模板
public class MailUtils { public static SimpleMailMessage makeSimpleMessage(String subject,String text){ SimpleMailMessage simpleMessage = new SimpleMailMessage(); simpleMessage.setFrom("aihehe123@foxmail.com"); simpleMessage.setTo("aihehe123@foxmail.com"); simpleMessage.setSubject(subject); simpleMessage.setText(text); return simpleMessage; } }
2、应用启动通知
@Component @Slf4j public class ApplicationStartHandler implements ApplicationListener<ContextRefreshedEvent> { @Autowired JavaMailSender javaMailSender; @Override public void onApplicationEvent(ContextRefreshedEvent event) { javaMailSender.send(MailUtils.makeSimpleMessage("应用启动通知","")); log.info("app start mail sended"); } }
3、应用停止通知;
public class XXXApplication { @Autowired JavaMailSender javaMailSender; public static void main(String[] args) { SpringApplication.run(XXXApplication.class, args); } @PreDestroy public void onExit() { log.info("###STOPing###"); javaMailSender.send(MailUtils.makeSimpleMessage("应用停止通知", "")); log.info("###STOP FROM THE LIFECYCLE###"); } }
总结
对个人的一些小型应用来说,对比短信、Push、钉钉、微信等通知方式,个人认为邮箱是简单、免费、最有效的通知方式。
然后给了简单的Demo演示下如何配置Spring Boot支持发送邮件,以及给一个案例在应用启动和停止的时候发送邮件通知给自己;