springBoot学习之 发送邮件

邮件发送

发送邮件是一个很重要的服务,比如注册验证,忘记密码或者其他运营信息。之前使用javaMail API发送,在boot发送邮件已经做了封装。

1.依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.application.properties配置

#mail
spring.mail.default-encoding=utf-8
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.username=xxx
spring.mail.password=xx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

3.邮件发送类和模板

  • 发送纯文本
  • 发送html文件 (带图片,附件)
  • 使用模板发送
@Component
public class MailUtil {
    private static final Logger logger = LoggerFactory.getLogger(MailUtil.class);
    @Value("${spring.mail.username}")
    private String user;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    public Configuration configuration;
    /**
     *
     * @param receiver
     * @param subject
     * @param content
     * @throws Exception
     */
    public void send(String receiver,String subject,String content) throws Exception{
        logger.info("收件人:"+receiver);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(user);
        message.setTo(receiver);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    public void sendHtml(String receiver,String subject,String content) throws  Exception{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(user);
        helper.setTo(receiver);
        helper.setSubject(subject);
        helper.setText(content,true);

        //图片
        File file = ResourceUtils.getFile("classpath:images/mail/a.png");
        helper.addInline("MyImg",file);
        // 发送附件
        file = ResourceUtils.getFile("classpath:attach/a.txt");
        helper.addAttachment("荒野",file);
        mailSender.send(message);
    }

    public void sendFreemark(String receiver,String subject,String tmp) throws Exception{
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom(user);
        helper.setTo(receiver);
        helper.setSubject(subject);


        Map<String,Object> data  = new HashMap<>();
        data.put("text","freemark test");
        data.put("author","lg");
        Template template = configuration.getTemplate(tmp);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template,data);

        helper.setText(text,true);
        mailSender.send(message);
    }
}

4.测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
    @Autowired
    private MailUtil mailUtil;

    @Test
    public void send() throws Exception{
        mailUtil.send("xxx","springboot mail test","boot 邮件测试");
    }

    @Test
    public void sendHtml() throws  Exception{
        String content = "<html><body><h3>发送html测试</h3><img src=\"cid:MyImg\"></body></html>";
        mailUtil.sendHtml("xxx","mail html test",content);
    }

    @Test
    public void sendFreemark() throws  Exception{
        mailUtil.sendFreemark("xxx","mail html test","mail.flt");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot中,可以使用JavaMail来实现发送邮件的功能。首先,需要导入spring-boot-starter-mail的依赖\[1\]。然后,在application.properties配置文件中填入连接邮件服务器、提供端口号、发送邮件地址、发送方STMP授权码等信息\[2\]。接下来,可以使用SpringBoot提供的对象来使用邮件服务\[4\]。具体步骤如下: 1. 开启发送邮件的STMP服务。 2. 加入Mail依赖,使得SpringBoot项目支持邮件服务。 3. 配置连接邮箱服务器以及STMP服务的信息。 4. 使用SpringBoot提供的对象来发送邮件。 以下是一个简单的示例代码,用于在SpringBoot登录后发送邮件: ```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); } } ``` 在登录成功后,调用`sendEmail`方法即可发送邮件。需要注意的是,需要在`application.properties`中配置好邮件服务器的相关信息\[2\]。 希望这个回答对您有帮助! #### 引用[.reference_title] - *1* [SpringBoot整合邮件发送](https://blog.csdn.net/Learning_xzj/article/details/125511069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot 实现发送简单邮件](https://blog.csdn.net/rain67/article/details/126491348)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值