以163邮箱服务为例。
- 新建springbooot工程:testemail
- 编辑pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>testemail</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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>
</project>
- 新建启动类:TestEmailApp
package cn.sky;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestEmailApp {
public static void main(String[] args) {
SpringApplication.run(TestEmailApp.class,args);
}
}
- 新建EmailController类(方便起见,代码都写在controller类里)
package cn.sky.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* send1和send2都是通过set方法进行手动设置参数
* send3通过application.properties自动配置
*/
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private JavaMailSenderImpl javaMailSender;
/**
* 使用25端口
* @return
*/
@RequestMapping("/send1")
public Object send1(){
try{
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("发件人@163.com");
javaMailSender.setPassword("这是网易邮箱的授权码");
javaMailSender.setHost("smtp.163.com");
javaMailSender.setPort(25);
javaMailSender.setProtocol("smtp");
javaMailSender.setDefaultEncoding("UTF-8");
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", true);
mailProperties.put("mail.smtp.starttls.enable", true);
javaMailSender.setJavaMailProperties(mailProperties);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom("发件人@163.com");
mimeMessageHelper.setTo("收件人@qq.com");
mimeMessageHelper.setSubject("这是邮件主题");
mimeMessageHelper.setText("这是邮件内容", false);
javaMailSender.send(mimeMessage);
return "发送成功";
} catch (MessagingException e) {
e.printStackTrace();
}
return "发送失败";
}
/**
* 使用465端口,需要打开配置:mail.smtp.ssl.enable=true,否则报错
* @return
*/
@RequestMapping("/send2")
public Object send2(){
try{
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setUsername("发件人@163.com");
javaMailSender.setPassword("这是网易邮箱的授权码");
javaMailSender.setHost("smtp.163.com");
javaMailSender.setPort(465);
javaMailSender.setProtocol("smtp");
javaMailSender.setDefaultEncoding("UTF-8");
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", true);
mailProperties.put("mail.smtp.starttls.enable", true);
mailProperties.put("mail.smtp.ssl.enable", true);
javaMailSender.setJavaMailProperties(mailProperties);
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom("发件人@163.com");
mimeMessageHelper.setTo("收件人@qq.com");
mimeMessageHelper.setSubject("这是邮件主题");
mimeMessageHelper.setText("这是邮件内容", false);
javaMailSender.send(mimeMessage);
return "发送成功";
} catch (MessagingException e) {
e.printStackTrace();
}
return "发送失败";
}
/**
* 使用465端口,需要打开配置:mail.smtp.ssl.enable=true,否则报错
* @return
*/
@RequestMapping("/send3")
public Object send3(){
try{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMessageHelper.setFrom("发件人@163.com");
mimeMessageHelper.setTo("收件人@qq.com");
mimeMessageHelper.setSubject("这是邮件主题");
mimeMessageHelper.setText("这是邮件内容", false);
javaMailSender.send(mimeMessage);
return "发送成功";
} catch (MessagingException e) {
e.printStackTrace();
}
return "发送失败";
}
}
新建配置文件:application.properties
spring.application.name = testemail
server.port = 8080
#发件人邮箱地址
spring.mail.username = test@163.com
# 这里使用的是授权码,而不是邮箱的登录密码
spring.mail.password = 这是网易邮箱的授权码
spring.mail.default-encoding = utf-8
spring.mail.protocol = smtp
spring.mail.host = smtp.163.com
spring.mail.port = 25
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.timeout=5000
#如果端口配置为465,则需要同时配置spring.mail.properties.mail.smtp.ssl.enable=true
#spring.mail.port = 465
#spring.mail.properties.mail.smtp.ssl.enable=true
附1:网易163免费邮箱相关服务器信息
附2:开启smtp服务获取授权码
- 登录163邮箱:https://mail.163.com/
- 进入POP3/SMTP/IMAP设置页面
- 点击“开启”按钮
- 点击“继续开启”按钮
- 按要求发送短信,然后点击“我已发送””
- 页面将显示授权码(只显示一次,需要保管好)