SpringBoot 发送邮件

邮件协议基本知识

文末源码
  • 1.SMTP:简单邮件传输协议,用于发送邮件,默认端口25
  • 2.POP2: 邮局协议2,用于接收邮件,默认端口109,基本已废弃
  • 3.pop3: 邮局协议3,用于接收邮件,默认端口 110
  • 4.IMAP:网络信息访问协议,用于接收邮件,默认端口 143,只下载邮件标题 收件人信息

以QQ设置中为例:

在这里插入图片描述

代码演示

第一步,新建项目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
[email protected]
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("[email protected]",
                                        "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("[email protected]","Test",emailContent);

    }

成功收到邮件:
在这里插入图片描述

仓库地址

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>