《Spring in action 4》(十)Spring发送eMail

时间:2023-01-11 20:56:34


Spring发送eMail

文章目录

  • ​​Spring发送eMail​​
  • ​​邮件发送准备​​
  • ​​POM文件​​
  • ​​配置JavaMailSender​​
  • ​​项目结构图​​
  • ​​发送简单邮件​​
  • ​​发送带有附件的邮件​​
  • ​​发送HTML邮件​​
  • ​​发送模板邮件​​
  • ​​最后​​

邮件发送准备

导入mail以及spring context的依赖支持。

POM文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ooyhao.spring</groupId>
<artifactId>spring-in-action-19-01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>spring-in-action-19-01 Maven Webapp</name>
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>

<!--导入Servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>

<!--导入SpringMVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>

<!--导入Jackson依赖-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.8</version>
</dependency>

<!--Junit测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<!--hibernate参数校验依赖-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.0.Alpha3</version>
</dependency>

<!--整合Thymeleaf-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>

<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.11</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>

<!-- druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>

<!-- dbcp 数据源 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>

<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>

</dependencies>

<build>
<finalName>spring-in-action-19-01</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

配置JavaMailSender

@ComponentScan(basePackages = "com.ooyhao.spring", useDefaultFilters = true,
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class),
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = RestController.class)
}
)
public class RootConfig {
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.163.com");
mailSender.setUsername("ooyhao1996@163.com");
mailSender.setPassword("163mailcode");
mailSender.setDefaultEncoding("utf-8");
return mailSender;
}
}

项目结构图

《Spring in action 4》(十)Spring发送eMail

发送简单邮件

@Service
public class SendMailService {

@Autowired
private JavaMailSender mailSender;

/**
* 发送简单邮件消息
*/
public void sendSimpleMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("ooyhao1996@163.com");
message.setTo("625194996@qq.com");
message.setSubject("邮件");
message.setText("邮件");
message.setSentDate(new Date());
mailSender.send(message);
}
}

发送带有附件的邮件

@Service
public class SendMailService {

@Autowired
private JavaMailSender mailSender;

public void sendMailWithAttachment() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("ooyhao1996@163.com");
helper.setTo("625194996@qq.com");
helper.setSentDate(new Date());
helper.setSubject("带有附件的邮件");
helper.setText("这是一封具有附件的邮件");
/*可以携带多个附件*/
ClassPathResource resource1 = new ClassPathResource("images/123.png");
helper.addAttachment("123.png",resource1);
ClassPathResource resource2 = new ClassPathResource("images/456.png");
helper.addAttachment("456.png",resource2);
mailSender.send(mimeMessage);
}
}

发送HTML邮件

@Service
public class SendMailService {

@Autowired
private JavaMailSender mailSender;

/*发送一封HTML邮件*/
public void sendHtmlMail() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("ooyhao1996@163.com");
helper.setTo("625194996@qq.com");
helper.setSentDate(new Date());
helper.setSubject("HTML邮件");
helper.setText("<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"\t<title>HTML邮件</title>\n" +
"</head>\n" +
"<body>\n" +
"\t<h1>这是一份HTML邮件</h1>\n" +
"\t<img src=\"cid:logo\">\n" +
"</body>\n" +
"</html>",true);
ClassPathResource resource = new ClassPathResource("res/images/logo.png");
helper.addInline("logo",resource);
mailSender.send(mimeMessage);
}
}

发送模板邮件

使用模板邮件发送时,需要配置模板解析器和模板引擎。

@Bean
public ClassLoaderTemplateResolver emailTemplateResolver(){
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setPrefix("mail/");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("utf-8");
return resolver;
}

/*配置*/
@Bean
public SpringResourceTemplateResolver templateResolver(){
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setSuffix("/WEB-INF/templates/");
resolver.setSuffix(".html");
resolver.setCharacterEncoding("utf-8");
return resolver;
}


@Bean
public SpringTemplateEngine templateEngine(Set<ITemplateResolver> templateResolvers){
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolvers(templateResolvers);
return engine;
}
@Service
public class SendMailService {

@Autowired
private JavaMailSender mailSender;

@Autowired
private SpringTemplateEngine springTemplateEngine;

/*发送一封使用Thymeleaf模板生成的邮件*/
public void sendTemplateHTML() throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom("ooyhao1996@163.com");
helper.setTo("625194996@qq.com");
helper.setSentDate(new Date());
helper.setSubject("HTML邮件");
Context context = new Context();
context.setVariable("firstTitle","这是一封模板生成的邮件");
context.setVariable("secondTitle","模板邮件|ouyanghao");
context.setVariable("src","https://ooyhao.gitee.io/vuepress-blog/logo.png");
String emailText = springTemplateEngine.process("emailTemplate.html", context);
helper.setText(emailText,true);
mailSender.send(mimeMessage);
}
}

HTML模板

<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${firstTitle}">一级标题</h1>
<h2 th:text="${secondTitle}">二级标题</h2>
<img th:src="${src}"><!--这里需要填写公网地址,localhost无法显示 -->

</body>
</html>

发送HTML(Thymeleaf)模板邮件时,需要注意一些细节,比如上述代码中的图片显示,则需要用一张外网可以访问到的图片,否则邮件中无法正常显示。

总结:

本节重要是练习如何使用Spring中的简单方法来实现邮件发送。并且包含了简单邮件、附件邮件、HTML邮件以及模板邮件。

源码:

​https://gitee.com/ooyhao/JavaRepo_Public/tree/master/Spring-in-Action/spring-in-action-19/​

最后

如果觉得不错的话,那就关注一下小编哦!一起交流,一起学习

《Spring in action 4》(十)Spring发送eMail