SprongBoot发送邮件(一)发送简单文本邮件HelloWorld

使用SpringBoot发送邮件首先要加入依赖:

        <!--发送邮件需要的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

然后在application.properties中配置以下信息

#发送邮件的协议以及主机,我用的QQ邮箱所以是smtp.qq.com,如果是126邮箱则应该改为smtp.126.com
spring.mail.host=smtp.qq.com
#发送邮箱的邮箱地址
spring.mail.username=824668713@qq.com
#移动客户端授权码(不是登录密码,需要到相应邮箱账户中获取)
spring.mail.password=zxvbyuabergtfg
#编码
spring.mail.default-encoding=UTF-8

然后在SpringBoot项目中新建包service,新建类MailService.java ,在类上面添加注解@Service

@Service
public class MailService {

}

注入JavaMailSender对象

    @Autowired
    private JavaMailSender javaMailSender;

以及注入配置文件中的username作为邮件的发件人

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

新建sendSimpleMail(String to, String subject, String content)方法如下:


    /**
     * @param to      邮件收件人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String to, String subject, String content) {

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        //设置邮件的发件人
        simpleMailMessage.setFrom(from);
        //设置收件人
        simpleMailMessage.setTo(to);
        //设置邮件的主题
        simpleMailMessage.setSubject(subject);
        //设置邮件的内容
        simpleMailMessage.setText(content);

        //通过JavaMailSender对象发送邮件信息
        javaMailSender.send(simpleMailMessage);

    }

使用junit测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {

    @Resource
    MailService mailService;

    @Test
    public void sendSimpleMailTest() {
        mailService.sendSimpleMail("824668713@qq.com", "SpringBoot HelloWorld", "HelloWorld!");
    }


}

使用SpringBoot发送简单文本邮件就完成啦!

Spring Boot 提供了简单而强大的邮件发送功能。以下是使用 Spring Boot 发送邮件的基本步骤: 1. 配置邮件发送参数:在 `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 ``` 2. 创建一个邮件发送服务类:创建一个 Java 类,用于封装邮件发送的逻辑。可以使用 Spring Boot 提供的 `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); } } ``` 3. 在需要发送邮件的地方调用邮件发送服务:在需要发送邮件的地方,注入邮件发送服务,并调用其方法发送邮件。例如: ```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("/send-email") public String sendEmail() { emailService.sendEmail("recipient@example.com", "Hello", "This is a test email"); return "Email sent"; } } ``` 以上是使用 Spring Boot 发送邮件的基本步骤。根据实际需求,你还可以使用模板引擎来发送 HTML 邮件,添加附件等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值