基于SpringBoot邮件发送

一.maven依赖:

<!-- 邮件发送 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>

二.配置文件:

##邮箱账号  我这里使用的是QQ邮箱哦
spring.mail.username= "yourMailId"
##邮箱密码(或者QQ邮箱授权码,QQ邮箱必须是授权码哦)
spring.mail.password= "yourMailPasswold"
##邮箱服务器地址
spring.mail.host=smtp.qq.com
##邮箱端口号
spring.mail.port=465
##编码格式
spring.mail.default-encoding=UTF-8

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

 三.实现:

1.普通发送邮件(测试类):

package com.example.demo.run.mail;


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@SpringBootTest
public class RunTextMail {

    @Autowired
    private JavaMailSender javaMailSender;


    @Test
    public void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("邮件主题");
        message.setText("邮件文本内容");
        //发件人邮箱账号
        message.setFrom("xxx@qq.com");
        //收件人邮箱账号
        message.setTo("yyy@qq.com");
        javaMailSender.send(message);

    }
}

2.异步定时发送带内联附件的邮件(业务类):

package com.example.demo.run.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class SchedulingMailService {

    @Autowired
    private JavaMailSender javaMailSender;

    //异步注解
    @Async
    //定时任务注解
    @Scheduled(cron="* * * * * ?")
    public void runSchedulingMail() throws MessagingException {

        //延时10s
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        //发件人邮箱账号
        mimeMessageHelper.setFrom("xxx@qq.com");

        //收件人邮箱账号
        mimeMessageHelper.setTo("yyy@qq.com");

        //邮件主题
        mimeMessageHelper.setSubject("异步发送邮箱哦");

        //邮件内容
        mimeMessageHelper.setText("<h1>我是内容,我也是html的h1标题,当然我还带了附件的呢</h1>",true);

        //获取本地系统资源
        FileSystemResource resource = new FileSystemResource(new File("F://test.png"));

        //内联
        mimeMessageHelper.addInline("test.png",resource);

        //附件
        mimeMessageHelper.addAttachment("text.png",resource);

        //发送
        javaMailSender.send(mimeMessage);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架。它提供了许多开箱即用的功能,包括邮件发送。下面是使用Spring Boot实现邮件发送的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Boot的邮件依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 2. 配置邮件信息:在application.properties或application.yml文件中配置邮件服务器的相关信息,例如: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=your-email@example.com spring.mail.password=your-email-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建邮件服务类:创建一个邮件服务类,用于发送邮件。可以使用JavaMailSender来发送邮件,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailService { @Autowired private JavaMailSender mailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); mailSender.send(message); } } ``` 4. 调用邮件服务类:在需要发送邮件的地方调用邮件服务类的sendEmail方法,例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailService emailService; @GetMapping("/sendEmail") public String sendEmail() { emailService.sendEmail("recipient@example.com", "Test Email", "This is a test email."); return "Email sent successfully."; } } ``` 以上就是使用Spring Boot实现邮件发送的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值