springboot实现给邮箱发送信息(邮件任务)

本文介绍了如何使用Springboot结合IntelliJ IDEA实现邮件发送功能,包括简单邮件和复杂邮件(含附件)的创建。通过引入spring-boot-starter-mail依赖,配置QQ邮箱属性,并利用JavaMailSenderImpl发送邮件。邮件内容可以包含HTML文本,同时支持添加多个附件。

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

springboot实现给邮箱发送信息(邮件任务)

开发工具与关键技术:mailSender/IntelliJ IDEA 
作者:老龙
撰写时间:2020/08/09

本文章基于springboot开发,开发工具IntelliJ IDEA ,话不多说,直接上干货
1.pom.xml导入mailSender的启动器

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

点击组件进去看,发现其实主要是依赖jakarta.mail这个jar包
在这里插入图片描述
接下来是配置属性文件,以properties为例(这里使用的是QQ邮箱)

spring.mail.username=3441817786@qq.com
spring.mail.password=msisilccohrwfiih
#QQ邮箱smtp.qq.com,163邮箱smtp.163.com,以此类推
spring.mail.host=smtp.qq.com
#开启加密验证,QQ邮箱需要,其他不需要
spring.mail.properties.mail.smtp.ssl.enable=true

spring.mail.password的密码是QQ邮箱SMTP加密过的,需要手动开启(因为使用明文密码贼危险,你可以使用明文的)
设置---->账号---->smtp
在这里插入图片描述

接下来先写一封最简单的邮件

import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@SpringBootTest
class Springboot09TestApplicationTests {
    @Autowired
    JavaMailSenderImpl mailSender;


    @Test
        //一个简单的邮件
    void contextLoads() {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //邮件主题
        mailMessage.setSubject("java你好");
        //邮件内容
        mailMessage.setText("ok,邮件任务走起!");
        //发送到。。。。
        mailMessage.setTo("xxxxxxxx@qq.com");
        //谁发的。。。。
        mailMessage.setFrom("3441817786@qq.com");
        mailSender.send(mailMessage);
    }
    }

在这里插入图片描述
下面是一封相对复杂的邮件

@Test
    void mailMessage() throws MessagingException, IOException {
        //一个复杂的邮件
        MimeMessage message= mailSender.createMimeMessage();

        //组装  multipart开启多文件上传
        MimeMessageHelper helper=new MimeMessageHelper(message,true);

        //正文
        helper.setSubject("来自XX的邮件");

        //html:true 开启html文本识别

        helper.setText("<div style=\"color: red\">这是一封复杂的邮件</div>",true);
        //添加附件
        helper.addAttachment("5.jpg",new File("C:\\Users\\JiaLong\\Pictures\\七濑胡桃\\5.jpg"));
        helper.addAttachment("33.jpg",new File("C:\\Users\\JiaLong\\Pictures\\七濑胡桃\\33.jpg"));
        helper.addAttachment("音乐.mp3",new File("C:\\Users\\JiaLong\\Music\\BGM\\Alan Walker - Routine [mqms2].mp3"));
       
        helper.setTo("xxxxxxx@qq.com");
        helper.setFrom("3441717786@qq.com");
        mailSender.send(message);
    }

在这里插入图片描述
注意:helper.addAttachment添加附件时,文件的附件名字要带文件后缀,不然传到邮箱的就是一个object文件,还有这是需要联网的,这是常识。。。。。。
上面添加附件时使用new 一个File文件,找到它的路径进行发送的,下面是接收IO流的形式发送

@Autowired
    private IOUtils ioUtils;
    
@Test
    void mailMessage() throws MessagingException, IOException {
        //一个复杂的邮件
        MimeMessage message= mailSender.createMimeMessage();

        //组装  multipart开启多文件上传
        MimeMessageHelper helper=new MimeMessageHelper(message,true);

        //正文
        helper.setSubject("来自XX的邮件");

        //html:true 开启html文本识别

        helper.setText("<div style=\"color: red\">这是一封复杂的邮件</div>",true);
       
        File file= new File("C:\\Users\\JiaLong\\Pictures\\沐.jpg");
        InputStream inputStream=new FileInputStream(file);
         //添加附件
       helper.addAttachment("这是一张沐浴的图片.jpg",new ByteArrayResource(ioUtils.toByteArray(inputStream)));
        helper.setTo("2955564688@qq.com");
        helper.setFrom("1147174244@qq.com");
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class IOUtils {
    public byte[] toByteArray(InputStream file) throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        int len=1024;
        int i;
        byte[] bytes=new byte[len];
        while ((i=file.read(bytes,0,len))>0){
            out.write(bytes,0,i);
        }
        return out.toByteArray();
    }
}

在这里插入图片描述
想了解更多的话可以点击源码进行了解,就很有趣

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值