有关于邮箱发送的java代码
String user = "1xxxxxxxx";
String code = "jasdifoherusdf";
String host = "smtp.qq.com";
boolean auth = true;
信息书写
application.yml
email:
user: 1xxxxxxxx
code: jasdifoherusdf
host: smtp.qq.com
auth: true
获取yml的值
@Value(“${键名}”)
@Value("${email.user}")
private String user;
@RequestMapping("/hello")
public String hello() {
return user;
}
如果觉得每次获取都需要前缀.属性比较麻烦,可以使用@ConfigurationProperties(prefix = "前缀")
,就不需要@Value()
了
- 要有实体类(实体类的成员属性要跟yml配置的键名一致)
package com.walker.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "email")
public class Email {
private String user;
private String code;
private String host;
private boolean auth;
}
- 使用
package com.walker.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
private String user;
@RequestMapping("/hello")
public String hello() {
return user;
}
}