Home >Java >javaTutorial >What is the method for springboot hutool to integrate email?
This article introduces two ways to send email files
spring boot integrates email to send files
spring boot uses hutool to integrate email to send files
To send email files you need to enable pop
<!--spring mail start--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
spring: mail: default-encoding: utf-8 host: smtp.aliyun.com username: 邮箱账号 password: 密码
package com.example.easypoi.rest; import lombok.RequiredArgsConstructor; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.GetMapping; 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.List; /** * @author ccubee * @since 2021/12/31 14:37 */ @RestController @RequestMapping("poi") @RequiredArgsConstructor public class ExcelRest { private final JavaMailSender javaMailSender; @GetMapping("bootSend") public String bootSend() { MimeMessage message = javaMailSender.createMimeMessage(); MimeMessageHelper messageHelper = null; try { messageHelper = new MimeMessageHelper(message, true, CharsetUtil.UTF_8); messageHelper.setTo("zt7086@126.com"); messageHelper.setFrom("ccubee@aliyun.com"); messageHelper.setSubject("subject"); // messageHelper.addAttachment( "book.xls", file); // true 表示启动HTML格式的邮件 messageHelper.setText("<html><head></head><body><h2>测试</h2></body></html>", true); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "成功"; } }
Hutool is an optional dependency on all third parties, so you need to introduce third-party dependencies yourself when using MailUtil.
Old version
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
New version
<dependency> <groupId>com.sun.mail</groupId> <artifactId>jakarta.mail</artifactId> <version>1.6.7</version> </dependency>
You can also directly daoru spring boot email dependency
<!--spring mail start--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
In the classpath (in the standard Maven project Create a new mail.setting
file in the config directory of src/main/resources
). The minimum configuration content is as follows. Under this configuration, both the SMTP server and user name will be identified through the from parameter. : form
user
is the file sending address user
of Alibaba Cloud mailbox is the complete address of the mailbox, that is, xxx@aliyun .com
# 邮件服务器的SMTP地址,可选,默认为smtp.<发件人邮箱后缀> host = smtp.yeah.net # 邮件服务器的SMTP端口,可选,默认25 port = 25 # 发件人(必须正确,否则发送失败) from = hutool@yeah.net # 用户名,默认为发件人邮箱前缀 user = hutool # 密码(注意,某些邮箱需要为SMTP服务单独设置授权码,详情查看相关帮助) pass = q1w2e3
Test
@GetMapping("testEmail") public String send() { MailUtil.send("123@qq.com", "测试", "邮件来自Hutool测试", false); return "ok"; }
You can also send files, just refer to the hutool
document
The above is the detailed content of What is the method for springboot hutool to integrate email?. For more information, please follow other related articles on the PHP Chinese website!