Spring Boot 完成HTML,带附件,带图片,模板邮件的发送

一、编写MailService类

package com.neo.helloWorld.hello;

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.Service;
import sun.management.FileSystem;

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

@Service
public class MailService {
    @Value("${spring.mail.username}")
    private String form;
    @Autowired
    private JavaMailSender mailSender;
    public void sayHello(){
        System.out.println("Hello World!");
    }

    //发送文本邮件
    public void sendSimpleMail(String to,String subject,String content){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(form);

        mailSender.send(message);
    }

    //发送HTML邮件
    public void sendHtmlMail(String to,String subject,String content) throws MessagingException{
        MimeMessage message=mailSender.createMimeMessage();


        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(form);

        mailSender.send(message);

    }

    //发送带附件的邮件
    public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws MessagingException{
        MimeMessage message=mailSender.createMimeMessage();

        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(form);

        FileSystemResource file=new FileSystemResource(new File(filePath));
        String fileName=file.getFilename();
        helper.addAttachment(fileName,file);

        mailSender.send(message);
    }

    //发送带图片的邮件
    public void sendInlinResourceMail(String to,String subject,String content,String rscPath,String rscId) throws MessagingException {
        MimeMessage message=mailSender.createMimeMessage();

        MimeMessageHelper helper=new MimeMessageHelper(message,true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content,true);
        helper.setFrom(form);

        FileSystemResource res=new FileSystemResource(new File(rscPath));
        helper.addInline(rscId,res);
        mailSender.send(message);
    }
}

二、模板邮件还需要新建HTML

emailTemplate.html




    
    邮件模板



您好!感谢您的注册,这是一封验证邮件,请点击下面的链接完成注册!

激活账号

三、pom.xml导入包(application.properties不变)


			org.springframework.boot
			spring-boot-starter-thymeleaf
		

四、实现测试类

package com.neo.helloWorld.service;

import com.neo.helloWorld.hello.MailService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.mail.MessagingException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
    @Resource
    MailService mailService;

    @Resource
    TemplateEngine templateEngine;
    @Test
    public void sayHelloTest(){
        mailService.sayHello();
    }

    @Test
    public void sendSimpleMailTest(){
        mailService.sendSimpleMail("[email protected]","这是第一封邮件","大家好,这是我的第一封邮件!");
    }

    @Test
    public void sendHtmlMailTest() throws MessagingException {

        String content="\n"+
                "\n"+
                "

hello world!这是一封HTML邮件

\n"+ "\n"+ ""; mailService.sendHtmlMail("[email protected]","这是一封HTML邮件",content); } @Test public void sendAttachmentsMail() throws MessagingException { String filePath="E://1.txt"; mailService.sendAttachmentsMail("[email protected]","这是一封带附件的邮件","这是一封带附件的邮件内容!",filePath); } @Test public void sendInlineResourceMailTest() throws MessagingException { String imgPath="E://1.jpg"; String rscId="neo001"; String content="\n"+ "\n"+ " 这是一封带图片的邮件:"+ "\n"+ ""; mailService.sendInlinResourceMail("[email protected]","这是一封带图片的邮件",content,imgPath,rscId); } @Test public void testTmeplateMailTest() throws MessagingException { Context context=new Context(); context.setVariable("id","006"); String emailContent=templateEngine.process("emailTemplate",context); mailService.sendHtmlMail("[email protected]","这是一封模板邮件",emailContent); } }

目录结构:
Spring Boot 完成HTML,带附件,带图片,模板邮件的发送_第1张图片

你可能感兴趣的:(Spring Boot 完成HTML,带附件,带图片,模板邮件的发送)