What is the method for springboot hutool to integrate email?
Content introduction
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
Prerequisite
To send email files you need to enable pop
spring boot integration Send email file
Import dependencies
<!--spring mail start--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
Configuration file
spring: mail: default-encoding: utf-8 host: smtp.aliyun.com username: 邮箱账号 password: 密码
Send email test
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 id="测试">测试</h2></body></html>", true); javaMailSender.send(message); } catch (MessagingException e) { e.printStackTrace(); } return "成功"; } }
spring boot Use hutool to integrate email Send email file
Import dependencies
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>
Configuration file
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor