1,导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.发送邮件
@Autowired JavaMailSenderImpl sender; @Async public void sendSimpleMail(){ SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setSubject("test"); mailMessage.setText("hello mail to "); mailMessage.setTo("@qq.com"); mailMessage.setFrom("@qq.com"); sender.send(mailMessage); } @Async public void sendMail(){ MimeMessage mimeMessage = sender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(mimeMessage,true); } catch (MessagingException e) { e.printStackTrace(); } try { helper.setSubject("test"); helper.setText("hello mail to "); helper.setTo("@qq.com"); helper.setFrom("@qq.com"); helper.addAttachment("1.jpg",new File("C:\\Users\\Pictures\\1.jpg")); } catch (MessagingException e) { e.printStackTrace(); } sender.send(mimeMessage); }