springboot-异步、发送邮件(一)

 pom.xml

<!--邮件javax.mail-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

 MyController.class

@Controller
public class MyController {
    @Autowired
    AsyncService asyncService;
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "你好";
    }
}

AsyncService.class

@Service
public class AsyncService {
    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("数据正在处理。。。。。。");
    }

}

SpringbootTaskApplication.class

@EnableAsync //开启异步注解功能
@SpringBootApplication
public class SpringbootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootTaskApplication.class, args);
    }
}

application.properties

spring.mail.username=110xxxxx@qq.com
#授权码
spring.mail.password=xxxxxxxxxxxxx
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

 

 SpringbootTaskApplicationTest.class

@SpringBootTest
class SpringbootTaskApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;
    @Test
    void contextLoads() {
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject("xx你好啊");
        message.setText("java这条路行下去");
        message.setTo("110xxxxx6@qq.com");
        message.setFrom("110xxxxx56@qq.com");
        mailSender.send(message);
    }

}

 

 

 

posted @ 2019-12-02 20:23  白石雁归南  阅读(1515)  评论(0编辑  收藏  举报