spring boot 发邮件mail
2017-09-04 本文已影响128人
帅哥_刷哥
1.QQ邮箱
1.1pom.xml配置
<!-- 邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
image.png
1.2application.properties配置
spring.mail.host=smtp.qq.com
spring.mail.username=476570365@qq.com
spring.mail.password=第三方密码// 这个东西需要到QQ
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
第三方密码// 这个东西需要到QQ邮箱去设置
http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
1.3项目目录
image.png1.4OneController.java
package com.shuai.spring_boot_1.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OneController {
@Autowired
private JavaMailSender mailSender;
@RequestMapping("/send")
public String send() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("476570365@qq.com");// 发送者.
message.setTo("1710665816@qq.com");// 接收者.
message.setSubject("测试邮件(邮件主题)");// 邮件主题.
message.setText("这是邮件内容");// 邮件内容.
mailSender.send(message);// 发送邮件
System.out.println("发送成功");
return "ok";
}
}
image.png
1.5App.java
package com.shuai.spring_boot_1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
image.png
1.6运行项目
运行App.java中的main方法
1.7访问项目
http://localhost:8080/send