实现邮件发送:简单邮件、附件邮件、嵌入资源的邮件、模板邮件
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter4-5-1</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-5-1</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-velocity</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,5 @@
|
||||
<html>
|
||||
<body>
|
||||
<h3>你好, ${username}, 这是一封模板邮件!</h3>
|
||||
</body>
|
||||
</html>
|
||||
@@ -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("<html><body><img src=\"cid:weixin\" ></body></html>", 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<String, Object> model = new HashedMap();
|
||||
model.put("username", "didi");
|
||||
String text = VelocityEngineUtils.mergeTemplateIntoString(
|
||||
velocityEngine, "template.vm", "UTF-8", model);
|
||||
helper.setText(text, true);
|
||||
|
||||
mailSender.send(mimeMessage);
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Reference in New Issue
Block a user