在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送。下面一起来看看Spring Boot如何发送邮件。
本文以163邮箱为例进行邮件发送功能,需要把用户信息以附件的形式发送,其他邮箱的配置也都大同小异。
一、邮件服务器与传输协议
要在网络上实现邮件功能,必须要有专门的邮件服务器。这些邮件服务器类似于现实生活中的邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中。
SMTP服务器地址:一般是 smtp.xxx.com,比如163邮箱是smtp.163.com,qq邮箱是smtp.qq.com。
SMTP协议:通常把处理用户smtp请求(邮件发送请求)的服务器称之为SMTP服务器(邮件发送服务器)。
POP3协议:通常把处理用户pop3请求(邮件接收请求)的服务器称之为POP3服务器(邮件接收服务器)。
二、Java发送邮件
2.1获取授权码
网易邮箱–>设置–>QQ邮箱–>邮箱设置–>账户–>POP3/SMTP/IMAP
开启POP3/SMTP服务,然后获取16位授权码(注意不要将授权码泄露,一个账户可以拥有多个授权码)
2.2添加依赖
<!--excel依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<!--easyexcel-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
<!--邮件依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--把配置文件的信息,读取并自动封装成实体类-->
<dependency>
<groupId >org.springframework.boot</groupId >
<artifactId >spring-boot-configuration-processor</artifactId >
<optional >true</optional >
</dependency>
2.3. 配置邮箱信息
需要注意的是password
不是邮箱登录密码,而是2.1中获取的授权码。
#文件下载路径
file:
path:
# window配置 D:/home/mail Linux: /home/app/mail
mail: D:/home/mail
spring:
mail:
protocol: smtps
from: xxx@163.com
host: smtp.163.com
username: xxxx@163.com
password: xxxxxxxxxx
port: 465
properties:
mail:
smtp:
auth: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
starttls:
enable: true
required: true
2.4. 发送邮件
与配置文件映射的实体类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="spring.mail")
@Data
public class MailConfiguartion {
private String protocol;
private String from;
private String host;
private String username;
private String password;
private String port;
}
controller
import com.baba.mybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/sendMail", method = RequestMethod.GET)
public void sendMail() throws IOException {
userService.sendMail();
}
}
service
import com.baba.mybatis.entity.MailConfiguartion;
import com.baba.mybatis.entity.User;
import com.baba.mybatis.mapper.UserMapper;
import com.baba.mybatis.service.UserService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Properties;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private MailConfiguartion mailConfiguartion;
@Value("${file.path.mail}")
private String downloadFilePath;
@Override
public void sendMail() throws IOException {
//查询生成excel文件所需要的数据
List<User> userList = userMapper.queryUserInfo();
//生成excel保存到downloadFilePath
File file = createFile(userList);
//to :邮件接收人
String to = "xxxxx@qq.com";
//发送邮件
sendMailFile(to, file);
}
void sendMailFile(String to, File file) {
// 创建邮件会话
Properties properties = new Properties();
properties.put("mail.transport.protocol", mailConfiguartion.getProtocol());
properties.put("mail.smtp.host", mailConfiguartion.getHost());
properties.put("mail.smtp.port", mailConfiguartion.getPort());
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(mailConfiguartion.getUsername(), mailConfiguartion.getPassword());
}
});
try {
// 创建邮件消息体
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailConfiguartion.getFrom()));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("用户信息通知");
// 创建多部分消息体
Multipart multipart = new MimeMultipart();
// 添加文本消息
BodyPart messageBodyPart = new MimeBodyPart();
StringBuffer sb=new StringBuffer();
sb.append("这是一封测试邮件").append("\n");
sb.append("所有用户信息见附件").append("\n");
messageBodyPart.setText(sb+"");
multipart.addBodyPart(messageBodyPart);
// 添加附件
messageBodyPart = new MimeBodyPart();
String filename = file.getName();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// 设置整个邮件消息体
message.setContent(multipart);
// 发送邮件
Transport.send(message);
System.out.println("邮件发送成功");
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
private File createFile(List<User> userList) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用户信息");
// 创建表头行
Row headerRow = sheet.createRow(0);
Cell headerCell1 = headerRow.createCell(0);
Cell headerCell2 = headerRow.createCell(1);
Cell headerCell3 = headerRow.createCell(2);
headerCell1.setCellValue("编号");
headerCell2.setCellValue("姓名");
headerCell3.setCellValue("年龄");
for (int i = 0; i < userList.size(); i++) {
// 创建数据行
Row dataRow1 = sheet.createRow(i + 1);
Cell dataCell1 = dataRow1.createCell(0);
Cell dataCell2 = dataRow1.createCell(1);
Cell dataCell3 = dataRow1.createCell(2);
dataCell1.setCellValue(userList.get(i).getId());
dataCell2.setCellValue(userList.get(i).getName());
dataCell3.setCellValue(userList.get(i).getAge());
}
// 文件目录不存在则创建文件夹及文件,若存在则覆盖文件
File file = new File(downloadFilePath + "/" + "用户信息.xlsx");
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
} else {
file.delete();
file.createNewFile();
}
FileOutputStream outputStream = new FileOutputStream(downloadFilePath + "/" + "用户信息.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
return file;
}
}
2.5. 发送效果
正文换行sb.append("这是一封测试邮件").append("\n");
或者sb.append("这是一封测试邮件").append("<br>");
now ,fight for future