zoukankan      html  css  js  c++  java
  • 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);
        }
    
    }

     

  • 相关阅读:
    Spring Data JPA条件查询
    Cannot add foreign key constraint
    项目运行慢的原因剖析
    文本摘要的一些研究概念
    datatables表格设置序号自增
    记录一次没有报错的错误
    final、static、this、super关键字总结
    datatables条件搜索后表格内出现重复字符串
    linux上重装redis,设置密码
    datatables render出现重复的字符
  • 原文地址:https://www.cnblogs.com/shiguanzui/p/11973058.html
Copyright © 2011-2022 走看看