mirror of
https://github.com/ityouknow/spring-boot-examples.git
synced 2026-09-07 01:27:25 +08:00
add mail service
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
package com.neo.service;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.thymeleaf.TemplateEngine;
|
||||
import org.thymeleaf.context.Context;
|
||||
|
||||
/**
|
||||
* Created by summer on 2017/5/4.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MailServiceTest {
|
||||
|
||||
@Autowired
|
||||
private MailService mailService;
|
||||
|
||||
@Autowired
|
||||
private TemplateEngine templateEngine;
|
||||
|
||||
@Test
|
||||
public void testSimpleMail() throws Exception {
|
||||
mailService.sendSimpleMail("ityouknow@126.com","test simple mail"," hello this is simple mail");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHtmlMail() throws Exception {
|
||||
String content="<html>\n" +
|
||||
"<body>\n" +
|
||||
" <h3>hello world ! 这是一封html邮件!</h3>\n" +
|
||||
"</body>\n" +
|
||||
"</html>";
|
||||
mailService.sendHtmlMail("ityouknow@126.com","test simple mail",content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sendAttachmentsMail() {
|
||||
String filePath="e:\\tmp\\application.log";
|
||||
mailService.sendAttachmentsMail("ityouknow@126.com", "主题:带附件的邮件", "有附件,请查收!", filePath);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sendInlineResourceMail() {
|
||||
String rscId = "neo006";
|
||||
String content="<html><body>这是有图片的邮件:<img src=\'cid:" + rscId + "\' ></body></html>";
|
||||
String imgPath = "C:\\Users\\summer\\Pictures\\favicon.png";
|
||||
|
||||
mailService.sendInlineResourceMail("ityouknow@126.com", "主题:这是有图片的邮件", content, imgPath, rscId);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sendTemplateMail() {
|
||||
//创建邮件正文
|
||||
Context context = new Context();
|
||||
context.setVariable("id", "006");
|
||||
String emailContent = templateEngine.process("emailTemplate", context);
|
||||
|
||||
mailService.sendHtmlMail("ityouknow@126.com","主题:这是模板邮件",emailContent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user