Springboot實現簡單傳送郵箱
阿新 • • 發佈:2018-12-12
首先建立一個郵箱,建議@126.com @163.com @qq.com都可以
開啟smtp,一下是使用圖解:
在pom.xml中引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
@SpringBootApplication
public class MialApplication {
public static void main(String[] args) {
SpringApplication.run(MialApplication.class, args);
}
}
package com.mial; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Title: MailController * @ProjectName mail * @Description: TODO * @Author: yufei * @Date 2018/9/26 11:52 */ @RestController public class MailController { @Autowired JavaMailSender jms; @GetMapping("/send") public String send() { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom("******@163.com"); mailMessage.setTo("******@qq.com"); mailMessage.setSubject("測試郵件"); mailMessage.setText("Hello World"); jms.send(mailMessage); return "1"; } }
在application.properties中:
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com
# 郵箱密碼
spring.mail.password=******
# 埠
spring.mail.port=25
# 協議
spring.mail.protocol=smtp
# 傳送郵箱的賬號
spring.mail.username=******@163.com
server.port=8081
執行main方法,訪問http://localhost:8081/send 出現1測試成功