Java实现邮件发送(包含发送附件及图片)

在项目开发中,除了需要短信验证外,有时候为了节省 短信费也会使用邮件发送。下面一起来看看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添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<!--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中获取的授权码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#文件下载路径
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  飞书邮箱发送时host改为smtp.feishu.cn
    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. 发送邮件

与配置文件映射的实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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>");

posted @   江南大才子  阅读(1008)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
点击右上角即可分享
微信分享提示