CHAPTER 1. Spring Boot—Introduction
1-3 Create a Spring Boot Application Using Spring Initializr
CHAPTER 2. Spring Boot—Basics
2-3 Testing
- See
org.junit.Assert
instead oforg.assertj.core.api.Assertions
-
https://junit.org/junit4/javadoc/4.8/org/junit/Assert.html
https://assertj.github.io/doc/
package com.apress.springbootrecipes.calculator.operation;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MultiplicationTest {
private final Multiplication addition = new Multiplication();
@Test
public void shouldMatchPlusSign() {
assertThat(addition.handles('*')).isTrue();
assertThat(addition.handles('/')).isFalse();
}
@Test
public void shouldCorrectlyApplyFormula() {
assertThat(addition.apply(2, 2)).isEqualTo(4);
assertThat(addition.apply(12, 10)).isEqualTo(120);
}
}
CHAPTER 8. Java Enterprise Services
8-3 Sending E-mail
Sending a Plain Text E-mail
- Spring Framework Documentation Integration 6. Email
-
https://docs.spring.io/spring/docs/5.1.6.RELEASE/spring-framework-reference/integration.html#mail
- JavaMailSender
package com.apress.springbootrecipes.mailsender;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.Message;
@SpringBootApplication
public class MailSenderApplication {
public static void main(String[] args) {
SpringApplication.run(MailSenderApplication.class, args);
}
@Bean
public ApplicationRunner startupMailSender(JavaMailSender mailSender) {
return (args) -> { mailSender.send((msg) -> {
var helper = new MimeMessageHelper(msg);
helper.setTo("recipient@some.where");
helper.setFrom("spring-boot-2-recipes@apress.com");
helper.setSubject("Status message");
helper.setText("All is well.");
});
};
}
}
Using Thymeleaf for E-mail Templates
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head>
<body>
<p><strong th:text="${msg}">Some email content will be here.</strong></p>
<p>
Kind Regards,
Your Application
</p>
</body>
</html>
package com.apress.springbootrecipes.mailsender;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import javax.mail.Message;
import java.util.Collections;
@SpringBootApplication
public class MailSenderApplication {
public static void main(String[] args) {
SpringApplication.run(MailSenderApplication.class, args);
}
@Bean
public ApplicationRunner startupMailSender(
JavaMailSender mailSender,
SpringTemplateEngine templateEngine) {
return (args) -> {
mailSender.send((msg) -> {
var helper = new MimeMessageHelper(msg);
helper.setTo("recipient@some.where");
helper.setFrom("spring-boot-2-recipes@apress.com");
helper.setSubject("Status message");
var context = new Context(
LocaleContextHolder.getLocale(),
Collections.singletonMap("msg", "All is well!"));
var body = templateEngine.process("email.html", context);
helper.setText(body, true);
});
};
}
}