1.在本地生成maven项目 在pom.xml 中配置
<!-- 依赖springboot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- springboot每一个框架的集成都是一个starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 配置发送邮箱 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
2.在src/main/resources中创建一个 application.properties的文件 ,在邮箱设置中查看是否开启了IMAP/SMTP服务,如果没开启就开启
#配置smtp的主机名
spring.mail.host=smtp.126.com
#配置发送方的邮件名
spring.mail.username=zmw960221@126.com
#配置发送方的授权密码
spring.mail.password=asd54215
#配置端口
spring.mail.port=25
spring.mail.protocol=smtp
3. 自定义一个controller层 调用
package cn.et;
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;
@RestController
public class MailController {
@Autowired
JavaMailSender jms;
@GetMapping("/send")
public String send(String emailto,String emailsubject,String emailcenter){
SimpleMailMessage smm= new SimpleMailMessage();
//设置发送方
smm.setFrom("zmw960221@126.com");
//设置收件方
smm.setTo(emailto);
//设置邮件标题
smm.setSubject(emailsubject);
//设置邮件内容
smm.setText(emailcenter);
jms.send(smm);
return "1";
}
}
4.创建一个页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="send">
接收人 :<input type="text" name="emailto"/><br/>
主题 :<input type="text" name="emailsubject"/><br/>
内容 :<textarea rows="20" cols="20" name="emailcenter"></textarea><br/>
<input type="submit" value="发送"/>
</form>
</body>
</html>
4.调用run方法,在浏览器上输入html路径
package cn.et;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//必须添加springbootApplication 启用spring的自动配置功能
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}