SpringBoot 发送邮件

邮件协议基本知识

文末源码

1.SMTP:简单邮件传输协议,用于发送邮件,默认端口25
2.POP2: 邮局协议2,用于接收邮件,默认端口109,基本已废弃
3.pop3: 邮局协议3,用于接收邮件,默认端口 110
4.IMAP:网络信息访问协议,用于接收邮件,默认端口 143,只下载邮件标题 收件人信息
以QQ设置中为例:
image.png

代码演示

第一步,新建项目SpringBoot工程。
第二步,引入maven依赖
发送邮件所需依赖,在pom.xml加入。

<!--mail-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

第三步,配置application.properties配置文件

spring.application.name=mail

spring.mail.host=smtp.qq.com
spring.mail.username=3050608872@qq.com
spring.mail.password=your_password  # 注意:这里是你的邮箱的第三方客户端密码,而不是邮箱的登录密码!
spring.mail.default-encoding=UTF-8

此处生成密码方法如下,点击邮箱设置,生成密码
在这里插入图片描述

发送带有照片的邮件

service

	// 将配置文件的username注入
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;
	/**
     * 发送带图片的邮件
     * @param Id
     * @param to
     * @param subject
     * @param content
     * @param id
     * @throws MessagingException
     */
    public void sendImgResourceMail(String to,
                                    String subject,
                                    String content,
                                    String Id, String id) {
        logger.info("发送带图片的邮件");
        MimeMessage mailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(mailMessage , true);


            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content , true);

            // 发    送     者
            helper.setFrom(from);

            // 添加图片
            FileSystemResource srcPath = new FileSystemResource(new File(Id));

            helper.addInline(Id , srcPath);

            javaMailSender.send(mailMessage);  // 发送邮件

        } catch (MessagingException e) {
            logger.info("发送带图片的邮件失败");
        }
    }

测试

@Test
    public void sendImgResourceMail() throws MessagingException {

        String srcPath = "F:\\img\\1.png";
        String Id = "666";
        String content = "<html><body><img src='cid:" + Id + "'/></body></html>";
        mailService.sendImgResourceMail("xxx@163.com",
                                        "Test",
                                        content,
                                        srcPath,
                                        Id);
    }

发送模板用例

<!--mail模板-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>邮件模板</title>
</head>
<body>
    你好,感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册,感谢您的支持!
    <!--/*@thymesVar id="id" type=""*/-->
    <a href="#" th:href="@{http://www.xxx.com/register/{id}(id=${id})}">激活账号</a>
</body>
</html>

测试

   @Autowired
    private TemplateEngine templateEngine;

    @Test
    public void TestTemplateMail() throws MessagingException {

        Context context = new Context();
        context.setVariable("id", "999");

        String emailContent = templateEngine.process("mailTemplate", context);

        mailService.sendHtmlMail("xxx@qq.com","Test",emailContent);

    }

成功收到邮件:

Github仓库地址: https://github.com/xmpjava/mail-java
Gitee仓库地址: https://gitee.com/love-code-bear/mail

posted @   大三的土狗  阅读(257)  评论(1编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示