SpringBoot整合(三)SpringBoot发送邮件

使用SpringBoot发送邮件

邮件发送其实是一个非常常见的需求,用户注册,找回密码等地方,都会用到,Spring Boot 中对于邮件发送,提供了相关的自动化配置类,使得邮件发送变得非常容易。

1、前置工作

目前国内大部分的邮件服务商都不允许直接使用用户名/密码的方式来在代码中发送邮件,都是要先申请授权码,这里以 QQ 邮箱为例,向大家演示授权码的申请流程:

首先我们需要先登录 QQ 邮箱网页版,点击上方的设置按钮:然后点击账户选项卡:在账户选项卡中找到开启POP3/SMTP选项,如下:

在这里插入图片描述

点击开启,开启相关功能,开启过程需要手机号码验证,按照步骤操作即可,不赘述。开启成功之后,即可获取一个授权码,将该号码保存好,一会使用。

2、引入依赖、配置邮箱基本信息

<!--集成发送邮件的功能-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

然后在yml配置文件中进行配置

spring:
  mail:
    host: smtp.qq.com # 设置邮箱主机
    port: 587 # SMTP 服务器的端口
    username: yyds@qq.com # 设置用户名
    password:  yyds # 设置密码,该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码

mail:
  from: ${spring.mail.username}
  to: yyds@163.com

做完这些之后,Spring Boot 就会自动帮我们配置好邮件发送类,相关的配置在

org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration 类中,部分源码如下:

@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class, MailSender.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {
    
}

可以看到,导入了另外一个配置 MailSenderPropertiesConfiguration 类,这个类中,提供了邮件发送相关的工具类,源码如下:

@Configuration
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
class MailSenderPropertiesConfiguration {
        private final MailProperties properties;
        MailSenderPropertiesConfiguration(MailProperties properties) {
                this.properties = properties;
        }
        @Bean
        @ConditionalOnMissingBean
        public JavaMailSenderImpl mailSender() {
                JavaMailSenderImpl sender = new JavaMailSenderImpl();
                applyProperties(sender);
                return sender;
        }
}

可以看到,这里创建了一个 JavaMailSenderImpl 的实例, JavaMailSenderImplJavaMailSender 的一个实现,我们将使用 JavaMailSenderImpl 来完成邮件的发送工作。

3、Service层代码

自定义的MailProperties配置类,用于解析mail开头的配置属性

package com.yyds.domain;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "mail")
public class MailProperties {

    private String from;

    private String to;

}

service层

package com.yyds.service;

import freemarker.template.TemplateException;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Map;

public interface MailService {

     void sendSimpleMail(String subject, String text) ;

     void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException;

     void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException;
}

package com.yyds.service.impl;

import com.yyds.domain.MailProperties;
import com.yyds.service.MailService;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private MailProperties myMailProperties;



    /**
     * 发送简单文本邮件
     */
    @Override
    public void sendSimpleMail(String subject, String text) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom(myMailProperties.getFrom());
        mailMessage.setTo(myMailProperties.getTo());

        mailMessage.setSubject(subject);
        mailMessage.setText(text);

        javaMailSender.send(mailMessage);
    }

    /**
     * 发送带有链接和附件的复杂邮件
     */
    @Override
    public void sendHtmlMail(String subject, String text, Map<String, String> attachmentMap) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        //是否发送的邮件是富文本(附件,图片,html等)
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);

        messageHelper.setFrom(myMailProperties.getFrom());
        messageHelper.setTo(myMailProperties.getTo());

        messageHelper.setSubject(subject);
        messageHelper.setText(text, true);//重点,默认为false,显示原始html代码,无效果

        if(attachmentMap != null){
            attachmentMap.entrySet().stream().forEach(entrySet -> {
                try {
                    File file = new File(entrySet.getValue());
                    if(file.exists()){
                        messageHelper.addAttachment(entrySet.getKey(), new FileSystemResource(file));
                    }
                } catch (MessagingException e) {
                    e.printStackTrace();
                }
            });
        }

        javaMailSender.send(mimeMessage);
    }


    /**
     * 发送模版邮件
     */
    @Override
    public void sendTemplateMail(String subject, Map<String, Object> params) throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        helper.setFrom(myMailProperties.getFrom());
        helper.setTo(myMailProperties.getTo());

        freemarker.template.Configuration configuration = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_19);
        TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass(), "/templates/");
        configuration.setTemplateLoader(templateLoader);
        Template template = configuration.getTemplate("mail.ftl");
        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);


        helper.setSubject(subject);
        helper.setText(html, true);//重点,默认为false,显示原始html代码,无效果

        javaMailSender.send(mimeMessage);
    }
}

4、发送邮件

4.1 测试发送简单文本邮件

@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {

    @Autowired
    private MailService mailService;

    @Test
    public void sendMail() {
        mailService.sendSimpleMail("测试Springboot发送邮件", "发送邮件...");
    }
}

在这里插入图片描述

4.2 测试发送带有链接和附件的复杂邮件

package com.yyds;

import com.yyds.service.MailService;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {

    @Autowired
    private MailService mailService;


    @Test
    public void testMail() throws MessagingException {

        Map<String, String> attachmentMap = new HashMap<>();
        attachmentMap.put("附件", "D:\\D_ENL_MRO数据统计.xlsx");

        mailService.sendHtmlMail("测试Springboot发送带附件的邮件2", "欢迎进入<a href=\"http://www.baidu.com\">百度首页</a>", attachmentMap);

    }

}

在这里插入图片描述

4.3 测试发送发送模版邮件

首先需要引入 Freemarker 依赖:

		<!--整合freemarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

然后在 resources/templates 目录下创建一个 mail.ftl 作为邮件发送模板:

在这里插入图片描述

<html>
    <body>
    <h3>你好, <span style="color: red;">${username}</span>, 这是一封模板邮件!</h3>
    </body>
</html>
package com.yyds;

import com.yyds.service.MailService;
import freemarker.template.TemplateException;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest(classes = BootStartApplication.class)
public class MimeMailTest {

    @Autowired
    private MailService mailService;



    @Test
    public void testFreemarkerMail() throws MessagingException, IOException, TemplateException {

        Map<String, Object> params = new HashMap<>();
        params.put("username", "Tom");

        mailService.sendTemplateMail("测试Springboot发送模版邮件", params);
    }

}

在这里插入图片描述

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Spring Boot整合发送邮件可以使用 JavaMailSender 接口来实现。下面是一个简单的示例代码: 首先,确保在你的项目的 pom.xml 文件中添加了 Spring Boot 邮件依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> ``` 然后,在你的 application.properties 或 application.yml 文件中配置邮件相关的属性: ```properties # 邮件发送服务器主机名 spring.mail.host=your-smtp-server # 邮件发送服务器端口号 spring.mail.port=your-smtp-port # 邮件发送服务器用户名 spring.mail.username=your-username # 邮件发送服务器密码 spring.mail.password=your-password # 是否启用 SSL/TLS 安全连接 spring.mail.properties.mail.smtp.starttls.enable=true ``` 接下来,创建一个邮件服务类,用于发送邮件: ```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 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); } } ``` 最后,在你的控制器或其他地方调用邮件服务类的 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("/send-email") public String sendEmail() { String to = "recipient@example.com"; String subject = "Test Email"; String text = "This is a test email sent from Spring Boot."; emailService.sendEmail(to, subject, text); return "Email sent successfully"; } } ``` 这样,当你访问 `/send-email` 路径时,就会发送一封测试邮件到指定的收件人邮箱。你可以根据实际需求修改相应的参数和邮件内容。记得将 `your-smtp-server`、`your-smtp-port`、`your-username` 和 `your-password` 替换为你的邮件服务器和账户信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值