Development tools: STS code download Link: https://github.com/theIndoorTrain/Springboot/tree/0d6194d6ea2d7f4e19791a3d3f3167f861f6453d preface:
In the previous blog post, we wrote Simple Mail and sent mail with attachments.
In this section, we're going to write a template email to send.
What is a template message?
Template mail:
We define the subject of the message, encapsulate it into an HTML, and dynamically change some of the values, such as the username ....
Then our email content is not required to be defined each time it is sent.
And this way, it is advantageous to the decoupling of the system, more convenient template mail content to improve and change.
In Sproingboot, the supported front-end templates are Freemarker and thymeleaf.
Here we use thymeleaf to achieve.
Template Mail Send
We assume that an activation message is sent to the user
1. Add Dependencies:
1 <!--Add thymeleaf Dependency -2 <Dependency>3 <groupId>Org.springframework.boot</groupId>4 <Artifactid>Spring-boot-starter-thymeleaf</Artifactid>5 </Dependency>
2. Add a Template:
1 <!DOCTYPE HTML>2 <HTMLLang= "zh"xmlns:th= "http://www.thymeleaf.org">3 <Head>4 <MetaCharSet= "UTF-8" >5 <title>Account Activation Email</title>6 </Head>7 <Body>8Hello, this is the verification email, please click the link below to complete the verification,<BR/>9 <ahref="#"Th:href= "@{Http://www.xm.com/eamil/{id} (Id=${id})}">Activate account</a>Ten </Body> One </HTML>
3. Define the Send template Mail business interface:
1 /** 2 * Send template mail 3 @param to : Recipient 4 @param Subject: Title 5 @param info : template name 6 */ 7 void sendtemplatemail (String to,string subject,string info);
4. Implement Send template Mail service:
Implementation process:
(1) Implementing an Interface (2) defining a template parser (3) defining parameters (4) binding parameters to templates (6) Parsing templates for strings (6) Encapsulating letters (7) sending messages
1 PackageCom.xm.service.impl;2 3 ImportJava.io.File;4 5 Importjavax.mail.MessagingException;6 ImportJavax.mail.internet.MimeMessage;7 8 Importorg.springframework.beans.factory.annotation.Autowired;9 ImportOrg.springframework.beans.factory.annotation.Value;Ten ImportOrg.springframework.mail.SimpleMailMessage; One ImportOrg.springframework.mail.javamail.JavaMailSender; A ImportOrg.springframework.mail.javamail.MimeMessageHelper; - ImportOrg.springframework.stereotype.Service; - ImportOrg.thymeleaf.TemplateEngine; the ImportOrg.thymeleaf.context.Context; - - ImportCom.xm.service.EmailService; - /** + * Mail delivery service Implementation - * @authorXM + * A */ at @Service - Public classEmailserviceimplImplementsEmailservice { - - //Get sender Information -@Value ("${mail.from.addr}") - PrivateString from; in - //Define Mail Sender to @Autowired + PrivateJavamailsender Sender; - the @Autowired * //Template Parsing $ Privatetemplateengine Templateengine;Panax Notoginseng - the + @Override A Public voidSendtemplatemail (string to, string subject, string info) { the //Create multi-purpose Internet Mail +MimeMessage message =sender.createmimemessage (); - $ Try { $ //Encapsulating Multi-purpose Internet Mail -Mimemessagehelper helper =NewMimemessagehelper (Message,true); - Helper.setfrom (from); the Helper.setto (to); - Helper.setsubject (subject);Wuyi //Encapsulating Template Parameters theContext context =NewContext (); -Context.setvariable ("id", 1); Wu //parsing Templates -String content =templateengine.process (info, context); About $ //true: HTML is recognized -Helper.settext (Content,true); - -}Catch(Exception e) { A e.printstacktrace (); + } the sender.send (message); - } $ the}
5. Define the test:
1 @Test 2 /** 3 * Send template message 4 */ 5 Public void Sendtemplatemail () {6 emailservice.sendtemplatemail ("[Email protected]", "Activate Mail", " Emailactivation "); 7 }
Test results:
2018-07-16
4. Springboot------Mail (2)