基于 SpringBoot、Velocity 发送 HTML 格式的邮件,带附件

SpringBoot 2.x 已经废除了spring-boot-starter-velocity组件,若想使用,必须单独添加依赖。

Maven核心依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.icegreen</groupId>
        <artifactId>greenmail</artifactId>
        <version>1.5.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.6.4</version>
    </dependency>
</dependencies>
核心代码
import org.apache.velocity.app.VelocityEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class VelocityConfig {
    
    @Bean
    public VelocityEngine velocityEngine() throws Exception {
        Properties properties = new Properties();
        properties.setProperty("input.encoding", "UTF-8");
        properties.setProperty("output.encoding", "UTF-8");
        properties.setProperty("resource.loader", "class");
        properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        VelocityEngine velocityEngine = new VelocityEngine(properties);
        return velocityEngine;
    }

}
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.StringWriter;

@Service
public class EmailService {
    @Autowired
    private VelocityEngine velocityEngine;
    @Autowired
    private JavaMailSender javaMailSender;

    public String sendMail() throws Exception {
        String textHtml = prepareText();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 若发生附件,第二个参数必须是true
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom("jinjiankang1980@126.com");
        helper.setSubject("邮件主题Velocity");
        // 若是HTML格式,第二个参数必须是true
        helper.setText(textHtml, true);
        helper.setTo("24560752@qq.com");
        helper.addAttachment("鲁棒图.jpg", new File("D:\\鲁棒图.jpg"));
        helper.addAttachment("鲁棒图原则.jpg", new File("D:\\鲁棒图原则.jpg"));
        javaMailSender.send(mimeMessage);
        return "Sent";
    }

    public String prepareText() throws Exception {
        VelocityContext context = new VelocityContext();
        context.put("username", "小明");
        context.put("age", 28);
        StringWriter stringWriter = new StringWriter();
        velocityEngine.mergeTemplate("templates/emails/welcome2.vm", "UTF-8", context, stringWriter);
        String text = stringWriter.toString();
        return text;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值