SpringBoot发送邮件

SpringBoot项目登录模块之发送邮件

发送邮件步骤是


1.邮箱设置  启动客户端SMTP服务

2.Spring Email
    --导入jar包
    --邮箱参数设置
    --使用javaMailSender发送邮件

3.模板引擎
  使用Thymeleaf发送HTML邮件

1.邮箱设置

打开邮箱设置中的SMTP设置,并且复制授权码,后续会用到

2.SpringEmail

导入依赖(在poxml文件中导入依赖)

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>2.3.9.RELEASE</version>
        </dependency>
  • 配置邮箱的相关参数
    // 在application.properties文件中设置相关参数
    spring.mail.host=smtp.sina.com
    spring.mail.port=465
    spring.mail.username=changml6@sina.com
    spring.mail.password=45c51a34bf89cb20//授权码
    spring.mail.protocol=smtps
    spring.mail.properties.mail.smtp.ssl.enable = true
  • 使用JavaMailSender发送邮件
     首先创建EmailClient.java
    @Component
    public class MailClient {
        private static final Logger logger = LoggerFactory.getLogger(MailClient.class);

        @Autowired//注入Mailsender
        private JavaMailSender mailSender;

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

        public void sendmail(String to,String subject,String content){
            try {
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);
                helper.setFrom(from);
                helper.setTo(to);
                helper.setSubject(subject);
                helper.setText(content,true);
                mailSender.send(helper.getMimeMessage());
            } catch (MessagingException e) {
                logger.error("发送邮件失败"+e.getMessage());
            }
        }
    }

使用Thymeleaf 生成模板HTML界面

在Resources的mail文件夹新建demo.html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>邮件示例</title>
    </head>
    <body>
        <p>欢迎你, <span style="color:red;" th:text="${username}"></span>!</p>
    </body>
    </html>

3.创建测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest
    @ContextConfiguration(classes = DemoApplication.class)
    public class MailTest {
        @Autowired
        private MailClient mailClient;

        @Autowired
        private TemplateEngine templateEngine;//模板引擎类

        @Test
        public void testTestMail(){//纯文本邮件发送
            mailClient.sendmail("changml6@sina.com","TEST","Welcome");//发送邮件
        }

        @Test
        public void TestHtmlMail(){//使用Thymeleaf模板发送
            Context context = new Context();
            context.setVariable("username","sunday");
            String content = templateEngine.process("/mail/demo",context);//生成相应的HTML页面并转换成String
            System.out.println(content);//打印生成的界面查看是否与上文新建的模板相同
            mailClient.sendmail("changml6@sina.com","Html",content);
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot发送邮件需要使用JavaMailSender接口来实现。以下是一个简单的示例代码: 首先,确保在你的项目中添加了相关依赖。在pom.xml文件中添加以下代码: ```xml <dependencies> <!-- Spring Boot Starter Mail --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies> ``` 然后,在你的应用程序中创建一个类来发送邮件。例如,你可以创建一个名为EmailSender的类: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Component; @Component public class EmailSender { @Autowired private JavaMailSender javaMailSender; public void sendEmail(String to, String subject, String text) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(text); javaMailSender.send(message); } } ``` 在上述示例中,我们使用了@Autowired注解来自动注入JavaMailSender对象,该对象是由Spring Boot自动配置提供的。 现在,你可以在你的应用程序的任何地方使用EmailSender类来发送邮件。例如,你可以在控制器中使用它: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class EmailController { @Autowired private EmailSender emailSender; @PostMapping("/sendEmail") public String sendEmail(@RequestBody EmailRequest emailRequest) { emailSender.sendEmail(emailRequest.getTo(), emailRequest.getSubject(), emailRequest.getText()); return "Email sent successfully!"; } } ``` 上述示例中,我们创建了一个名为EmailController的REST控制器,它接收一个包含收件人、主题和内容的EmailRequest对象,并使用EmailSender发送邮件。 请注意,你需要适当配置你的邮件服务器信息。在Spring Boot的application.properties(或application.yml)文件中添加以下配置: ```yaml spring.mail.host=your-mail-server spring.mail.port=your-mail-server-port spring.mail.username=your-username spring.mail.password=your-password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true ``` 以上是一个简单的示例,你可以根据你的实际需求进行修改和扩展。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值