thymeleaf填充html内容,Springboot Thymeleaf 发邮件 将html内容展示在邮件内容中

需要的依赖主要如下:

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-test

test

org.springframework.boot

spring-boot-starter-mail

代码如下:

主要文件有interface Mailserver 和 MailServerImpl

代码:

package com.example.demo.email;

public interface MailService {

void sendSimpleMail(String to, String subject, String content);

void sendHtmlMail(String to, String cc,String subject, String content);

void sendAttachmentsMail(String to, String subject, String content, String filePath);

}

package com.example.demo.email;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

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.Component;

import org.springframework.stereotype.Service;

import javax.mail.MessagingException;

import javax.mail.internet.MimeMessage;

import java.io.File;

@Component

//@Service

public class MailServiceImpl implements MailService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@Autowired

private JavaMailSender javaMailSender;

@Value("${spring.mail.username}")

private String from;

@Value("${spring.mail.bcc}")

private String bcc;

@Override

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 {

javaMailSender.send(message);

logger.info("发送简单邮件成功!");

} catch (Exception e) {

logger.error("发送简单邮件时发生异常!", e);

}

}

@Override

public void sendHtmlMail(String to, String cc,String subject, String content) {

MimeMessage message = javaMailSender.createMimeMessage();

try {

//true表示需要创建一个multipart message

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(from);

helper.setTo(to);//邮件接收者

helper.setCc(cc);

helper.setBcc(bcc);

helper.setSubject(subject);//邮件主题

helper.setText(content, true);//邮件内容

FileSystemResource avatar = new FileSystemResource("image/Jasmine.png");

helper.addInline("avatar",avatar);

javaMailSender.send(message);

logger.info("发送HTML邮件成功!");

} catch (MessagingException e) {

logger.error("发送HTML邮件时发生异常!", e);

}

}

@Override

public void sendAttachmentsMail(String to, String subject, String content, String filePath) {

MimeMessage message = javaMailSender.createMimeMessage();

try {

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(from);

helper.setTo(to);

helper.setSubject(subject);

helper.setText(content, true);

FileSystemResource file = new FileSystemResource(new File(filePath));

String fileName = filePath.substring(filePath.lastIndexOf(File.separator));

helper.addAttachment(fileName, file);

javaMailSender.send(message);

logger.info("发送带附件的邮件成功!");

} catch (MessagingException e) {

logger.error("发送带附件的邮件时发生异常!", e);

}

}

}

如何调用

Context context = new Context();

context.setVariable("bugnum", "bugnum#"+bugNotifyBean.getId());

context.setVariable("pro_img", "avatar");

context.setVariable("bugdes", bugNotifyBean.getDescription());

context.setVariable("bugstatus", bugNotifyBean.getBugStatus());

context.setVariable("crname", bugNotifyBean.getCrname());

context.setVariable("crnum", bugNotifyBean.getCrnum());

context.setVariable("oname", bugNotifyBean.getOname());

context.setVariable("pname", bugNotifyBean.getPname());

context.setVariable("rca", bugNotifyBean.getRca());

context.setVariable("tasknum", bugNotifyBean.getTasknum());

context.setVariable("solution", bugNotifyBean.getSolution());

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

mailService.sendHtmlMail(developerEmail, testerEmail,"您好,有一个Bug要关注,谢谢!!", emailContent);

1. 买坑之旅

org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message;

nested exception is:

java.io.FileNotFoundException: image\Jasmine.png

; message exception details (1) are:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值