diff --git a/Chapter4-5-1/pom.xml b/Chapter4-5-1/pom.xml new file mode 100644 index 0000000..1640972 --- /dev/null +++ b/Chapter4-5-1/pom.xml @@ -0,0 +1,59 @@ + + + 4.0.0 + + com.didispace + Chapter4-5-1 + 1.0.0 + jar + + Chapter4-5-1 + Spring Boot project + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-mail + + + + org.springframework.boot + spring-boot-starter-velocity + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter4-5-1/src/main/java/com/didispace/Application.java b/Chapter4-5-1/src/main/java/com/didispace/Application.java new file mode 100644 index 0000000..2a14b75 --- /dev/null +++ b/Chapter4-5-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/Chapter4-5-1/src/main/resources/application.properties b/Chapter4-5-1/src/main/resources/application.properties new file mode 100644 index 0000000..be54b4f --- /dev/null +++ b/Chapter4-5-1/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.mail.host=smtp.qq.com +spring.mail.username=username@qq.com +spring.mail.password=password +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true +spring.mail.properties.mail.smtp.starttls.required=true \ No newline at end of file diff --git a/Chapter4-5-1/src/main/resources/templates/template.vm b/Chapter4-5-1/src/main/resources/templates/template.vm new file mode 100644 index 0000000..863856e --- /dev/null +++ b/Chapter4-5-1/src/main/resources/templates/template.vm @@ -0,0 +1,5 @@ + + +

你好, ${username}, 这是一封模板邮件!

+ + \ No newline at end of file diff --git a/Chapter4-5-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter4-5-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100644 index 0000000..9d1327d --- /dev/null +++ b/Chapter4-5-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,96 @@ +package com.didispace; + +import org.apache.commons.collections.map.HashedMap; +import org.apache.velocity.app.VelocityEngine; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.core.io.FileSystemResource; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.ui.velocity.VelocityEngineUtils; + +import javax.mail.internet.MimeMessage; +import java.io.File; +import java.util.Map; + + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +public class ApplicationTests { + + @Autowired + private JavaMailSender mailSender; + @Autowired + private VelocityEngine velocityEngine; + + @Test + public void sendSimpleMail() throws Exception { + + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom("dyc87112@qq.com"); + message.setTo("dyc87112@qq.com"); + message.setSubject("主题:简单邮件"); + message.setText("测试邮件内容"); + + mailSender.send(message); + } + + @Test + public void sendAttachmentsMail() throws Exception { + + MimeMessage mimeMessage = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + helper.setFrom("dyc87112@qq.com"); + helper.setTo("dyc87112@qq.com"); + helper.setSubject("主题:有附件"); + helper.setText("有附件的邮件"); + + FileSystemResource file = new FileSystemResource(new File("weixin.jpg")); + helper.addAttachment("附件-1.jpg", file); + helper.addAttachment("附件-2.jpg", file); + + mailSender.send(mimeMessage); + } + + @Test + public void sendInlineMail() throws Exception { + + MimeMessage mimeMessage = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + helper.setFrom("dyc87112@qq.com"); + helper.setTo("dyc87112@qq.com"); + helper.setSubject("主题:嵌入静态资源"); + helper.setText("", true); + + FileSystemResource file = new FileSystemResource(new File("weixin.jpg")); + helper.addInline("weixin", file); + + mailSender.send(mimeMessage); + } + + @Test + public void sendTemplateMail() throws Exception { + + MimeMessage mimeMessage = mailSender.createMimeMessage(); + + MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); + helper.setFrom("dyc87112@qq.com"); + helper.setTo("dyc87112@qq.com"); + helper.setSubject("主题:模板邮件"); + + Map model = new HashedMap(); + model.put("username", "didi"); + String text = VelocityEngineUtils.mergeTemplateIntoString( + velocityEngine, "template.vm", "UTF-8", model); + helper.setText(text, true); + + mailSender.send(mimeMessage); + } + +} diff --git a/Chapter4-5-1/weixin.jpg b/Chapter4-5-1/weixin.jpg new file mode 100644 index 0000000..8979746 Binary files /dev/null and b/Chapter4-5-1/weixin.jpg differ