【Spring-boot】发送邮件

7 篇文章 0 订阅
4 篇文章 0 订阅

1 . pom文件加入依赖

<dependency>
 <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--发送模版文件使用,如不需要可以去掉-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2 . application.properties加入邮箱配置

spring.mail.host=邮件host(如:smtp.exmail.qq.com)
spring.mail.username=用户名
spring.mail.password=密码
spring.mail.default-encoding=UTF-8

3 . 发送邮件代码

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author: 
 * @create: 
 **/
@Slf4j
@Component
public class MailService {

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String from;

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);

        try {
            mailSender.send(message);
            log.info("简单邮件发送成功。");
        } catch (Exception e) {
            log.error("发送简单邮件时发生异常!", e);
        }

    }

    public void sendHtmlMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();

        try {
            //true表示需要创建一个multipart message
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            mailSender.send(message);
            log.info("html邮件发送成功");
        } catch (MessagingException e) {
            log.error("发送html邮件时发生异常!", e);
        }
    }

    public void sendMailUseTemp(WarnBatchDto warnBatchDto) {
        //创建邮件正文
        Context context = new Context();
        context.setVariable("name", warnBatchDto.getNickname());
        context.setVariable("gender", warnBatchDto.getGender());

        String emailContent = templateEngine.process("warnTemplate", context);

        sendHtmlMail(warnBatchDto.getEmail(), "测试", emailContent);

    }
}

4 . 模版文件

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
</head>
<body>
尊敬的<span th:text="${name}"></span>
<span th:switch="${gender}">
    <span th:case="0"></span>
    <span th:case="1">先生</span>
    <span th:case="2">女士</span>
</span>:<br/>
  您好<br/>

</body>
</html>

参考:http://www.ityouknow.com/springboot/2017/05/06/springboot-mail.html

Spring Boot Starter Mail是一个轻量级的启动器,它包含了发送电子邮件所需的基本依赖,使得在Spring Boot应用中集成邮箱功能变得更加简单。要使用这个组件,首先你需要添加`spring-boot-starter-mail`到你的项目的`pom.xml`或者`build.gradle`文件中。 以下是基本步骤: 1. 添加依赖:在Maven中,添加到`<dependencies>`标签内: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 在Gradle中,添加到`dependencies`块中: ```groovy implementation 'org.springframework.boot:spring-boot-starter-mail' ``` 2. 配置邮件服务器:在`application.properties`或`application.yml`中设置发件人的邮箱地址、SMTP服务器信息,例如: ```properties spring.mail.host=smtp.example.com spring.mail.port=587 spring.mail.username=myemail@example.com spring.mail.password=mypassword spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 3. 创建Java配置类,如`MailSenderConfiguration`,并注入`JavaMailSender`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; @Configuration public class MailSenderConfig { @Autowired private JavaMailSender javaMailSender; // 省略其他配置方法... } ``` 4. 实现发送邮件的Service或者Repository,调用`javaMailSender`实例的方法,比如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; @Autowired private JavaMailSender mailSender; public void sendEmail(SimpleMailMessage message) { mailSender.send(message); } ``` 5. 使用`SimpleMailMessage`构造函数构建邮件内容: ```java SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo("recipient@example.com"); mailMessage.setSubject("测试邮件"); mailMessage.setText("这是一封测试邮件的内容."); sendEmail(mailMessage); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值