springboot实现发送邮件,java发邮件

本文介绍如何使用SpringBoot实现批量发送邮件的功能。首先需要具备Java和SpringBoot的基础知识,并拥有163邮箱。接着,通过IDEA创建SpringBoot项目并引入相关依赖。配置邮件服务器参数后,利用JavaMailSenderImpl实现文本和HTML邮件的发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近期学习java后台开发,学到了springboot发送邮件。感觉这也算是一个很实用的知识点,就在这里总结下,方便以后用。也方便大家学习。 #准备工作

  • 1,有一点java基础和springboot基础
  • 2,有163邮箱 #步骤
  • 1,用idea新建springboot项目,如果不知道怎么用idea新建springboot项目请自行百度
  • 2,建好项目后在pom.xml文件中引入
<dependency>
      <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>
复制代码
  • 3,在配置文件application.properties中添加如下配置
spring.mail.host=smtp.163.com
spring.mail.username=你的163邮箱
spring.mail.password=注意这里不是邮箱密码,而是SMTP授权密码
spring.mail.port=25
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
复制代码

上面提到的smtp的授权密码的获取下面给出图解

注意:你这里设置的授权密码就是商品配置信息中要输入的密码

  • 4,下面上代码
package com.qcl;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;

import javax.mail.internet.MimeMessage;

/*
* 小石头
* 2501902696@qq.com
* */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootJmsApplicationTests {

    @Test
    public void contextLoads() {
    }


    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 发送包含简单文本的邮件
     */
    @Test
    public void sendTxtMail() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        // 设置收件人,寄件人
        simpleMailMessage.setTo(new String[]{"2501902696@qq.com", "1587072557@qq.com"});
        simpleMailMessage.setFrom("15611823564@163.com");
        simpleMailMessage.setSubject("Spring Boot Mail 邮件测试【文本】");
        simpleMailMessage.setText("这里是一段简单文本。");
        // 发送邮件
        mailSender.send(simpleMailMessage);

        System.out.println("邮件已发送");
    }

    /**
     * 发送包含HTML文本的邮件
     *
     * @throws Exception
     */
    @Test
    public void sendHtmlMail() throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        mimeMessageHelper.setTo("2501902696@qq.com");
        mimeMessageHelper.setFrom("15611823564@163.com");
        mimeMessageHelper.setSubject("Spring Boot Mail 邮件测试【HTML】");

        StringBuilder sb = new StringBuilder();
        sb.append("<html><head></head>");
        sb.append("<body><h1>spring 邮件测试</h1><p>hello!this is spring mail test。</p></body>");
        sb.append("</html>");

        // 启用html
        mimeMessageHelper.setText(sb.toString(), true);
        // 发送邮件
        mailSender.send(mimeMessage);

        System.out.println("邮件已发送");

    }

}
复制代码

我这里是用单元测试直接跑的。下面看下接收到的效果

到此,我们就轻松的实现了批量发送邮件的代码了。用springboot做邮件发送是不是特别简单。

转载于:https://juejin.im/post/5a37908ff265da43133d406f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值