Springboot郵件模板選擇及原因
阿新 • • 發佈:2018-11-29
一.Springboot郵件模板可選項
- Thymeleaf
- Freemarker
- Velocity
二.測試程式碼
1.Thymeleaf模板
-
模板部分
<!DOCTYPE html> <!-- 使用xmlns宣告為thymeleaf模板 --> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>郵件模板</title
-
測試部分
@Test public void testTemplateMailTest() throws MessagingException { Context context = new Context(); context.setVariable("info","您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!"); long startTime =
2.Freemarker模板
-
模板部分
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>郵件模板</title> </head> <body> ${(info)} </body> </html>
-
測試部分
@Test public void testFreeMarker() throws IOException, TemplateException { Map<String, Object> model = new HashMap<>(); model.put("info", "您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!"); long startTime = System.currentTimeMillis(); Template template = configurer.getConfiguration().getTemplate("emailFreeMarker.ftl"); String emailContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); long endTime = System.currentTimeMillis(); log.info("FreeMarker:\t"+(endTime-startTime)); log.info(emailContent); }
3.Velocity模板
-
模板部分
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>郵件模板</title> </head> <body> ${info} </body> </html>
-
測試部分
@Test public void testVelocity(){ Map<String,Object>model = new HashMap<>(); model.put("info", "您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!"); long startTime = System.currentTimeMillis(); String emailContent = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "emailVelocity.vm", "utf-8", model); long endTime = System.currentTimeMillis(); log.info("Velocity:\t"+(endTime-startTime)); log.info(emailContent); }
-
注:spring boot最新版已經不支援Velocity
三.測試結果
1.Thymeleaf模板[只列舉5次]
#No.1
2018-11-19 00:35:04.027 INFO 7157 --- [ main] c.example.demo.service.EmailServiceTest : Thymeleaf: 275
2018-11-19 00:35:04.027 INFO 7157 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<!-- 使用xmlns宣告為thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
<p>您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!</p>
</body>
</html>
2018-11-19 00:35:04.032 INFO 7157 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.2
2018-11-19 00:35:36.560 INFO 7160 --- [ main] c.example.demo.service.EmailServiceTest : Thymeleaf: 276
2018-11-19 00:35:36.560 INFO 7160 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<!-- 使用xmlns宣告為thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
<p>您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!</p>
</body>
</html>
2018-11-19 00:35:36.565 INFO 7160 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.3
2018-11-19 00:36:09.616 INFO 7167 --- [ main] c.example.demo.service.EmailServiceTest : Thymeleaf: 287
2018-11-19 00:36:09.616 INFO 7167 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<!-- 使用xmlns宣告為thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
<p>您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!</p>
</body>
</html>
2018-11-19 00:36:09.622 INFO 7167 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.4
2018-11-19 00:36:50.106 INFO 7171 --- [ main] c.example.demo.service.EmailServiceTest : Thymeleaf: 366
2018-11-19 00:36:50.106 INFO 7171 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<!-- 使用xmlns宣告為thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
<p>您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!</p>
</body>
</html>
2018-11-19 00:36:50.113 INFO 7171 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.5
2018-11-19 00:37:10.615 INFO 7173 --- [ main] c.example.demo.service.EmailServiceTest : Thymeleaf: 358
2018-11-19 00:37:10.615 INFO 7173 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<!-- 使用xmlns宣告為thymeleaf模板 -->
<html lang="en">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
<p>您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!</p>
</body>
</html>
2018-11-19 00:37:10.620 INFO 7173 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2.Freemarker模板[只列舉5次]
#No.1
2018-11-19 00:38:02.567 INFO 7183 --- [ main] c.example.demo.service.EmailServiceTest : FreeMarker: 85
2018-11-19 00:38:02.567 INFO 7183 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!
</body>
</html>
2018-11-19 00:38:02.573 INFO 7183 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.2
2018-11-19 00:38:22.807 INFO 7185 --- [ main] c.example.demo.service.EmailServiceTest : FreeMarker: 77
2018-11-19 00:38:22.807 INFO 7185 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!
</body>
</html>
2018-11-19 00:38:22.818 INFO 7185 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.3
2018-11-19 00:38:41.751 INFO 7187 --- [ main] c.example.demo.service.EmailServiceTest : FreeMarker: 75
2018-11-19 00:38:41.751 INFO 7187 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!
</body>
</html>
2018-11-19 00:38:41.758 INFO 7187 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.4
2018-11-19 00:39:07.605 INFO 7189 --- [ main] c.example.demo.service.EmailServiceTest : FreeMarker: 81
2018-11-19 00:39:07.605 INFO 7189 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!
</body>
</html>
2018-11-19 00:39:07.613 INFO 7189 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
#No.5
2018-11-19 00:39:27.704 INFO 7191 --- [ main] c.example.demo.service.EmailServiceTest : FreeMarker: 70
2018-11-19 00:39:27.705 INFO 7191 --- [ main] c.example.demo.service.EmailServiceTest : <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
您好,感謝您的註冊,這是一封驗證郵件,請點選下面的連線完成註冊,感謝您的支援!
</body>
</html>
2018-11-19 00:39:27.711 INFO 7191 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
3.結論
- 經執行結果可知,在模板生成方面Thymeleaf耗時200-300ms不等,而Freemarker耗時在100ms內,建議優先考慮Freemarker作為郵件模板