springboot发送邮件
springboot发送邮件
1.依赖文件pom.xml:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.配置文件:
#####163邮箱######## spring.mail.host=smtp.163.com spring.mail.username=xxxxxxx@163.com #163邮箱授权码 spring.mail.password=********* spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
3.controller:
@RestController public class HelloController { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.username}") private String username;
@RequestMapping("/email") public String sendSimpleMail() throws Exception { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(username); message.setTo("xxxxxxxx@qq.com"); message.setSubject("主题:发送邮件"); message.setText("测试邮件发送成功"); mailSender.send(message); return "success"; } }
如果报 535 认证失败,将下载后的local_policy.jar
和US_export_policy.jar
替换到JDK1.8的jre\lib\security
文件夹即可。
下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
程序员的眼里,不止有代码和bug,还有诗与远方和妹子!!!