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.jarUS_export_policy.jar替换到JDK1.8的jre\lib\security文件夹即可。 

下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

posted @ 2018-08-14 20:10  何其小静  阅读(500)  评论(0编辑  收藏  举报