MailSender: 是核心的发送邮件的接口
SimpleMailMessage: 可以设置邮件的发送者,以及接收者(接受者可以有多个)
具体很多Spring封装的类的用法参考官方文档:
http://docs.spring.io/spring/docs/4.2.5.RELEASE/spring-framework-reference/htmlsingle/#mail
1、首先项目是一个maven项目,在pom文件中加入starter的依赖:
<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>
2、在Spring Boot的配置文件里加入如下配置:
#application.yml
spring:
velocity:
resource-loader-path: classpath:/templates/
charset: UTF-8
mail:
host: smtp@163.com # 发送邮件的smtp服务器
username: ijiaobu # 用户名
password: so$nice # 登陆smtp服务器的密码
from: ijiaobu@163.com #注册的smtp服务器的邮箱
并定义对应的properties配置:
// MailProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "mail")
@Component
public class MailProperties {
private String host;
private String username;
private String password;
private String from;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
@Override
public String toString() {
return "EboxEmailProperties{" +
"host='" + host + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", from='" + from + '\'' +
'}';
}
}
3、JavaMailSender读取配置并注入到Spring的IOC容器中:
// MailingConfiguration.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;
@Configuration
public class MailingConfiguration {
@Autowired
private BulbEmailProperties portalEmailProperties;
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
javaMailSender.setProtocol("smtp");
javaMailSender.setHost(portalEmailProperties.getHost());
javaMailSender.setUsername(portalEmailProperties.getUsername());
javaMailSender.setPassword(portalEmailProperties.getPassword());
javaMailSender.setJavaMailProperties(getMailProperties());
return javaMailSender;
}
private Properties getMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.debug", "false");
return properties;
}
}
4、使用Velocity定义邮件模板:
在resources目录下:
Paste_Image.png
建立对应的模板文件:
<html>
<body>
<h3>Hi friend, we are testing registration confirmation :)!</h3>
Click this link to activate...........
<div>
<h1> <a href="${url}">${url}</a></h1>
</div>
${currentDate}
</body>
</html>
5、下面写一个controller测试一下吧,balabala
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
public class TestSendMail {
@Autowired
private BulbEmailProperties portalEmailProperties;
@Autowired
private JavaMailSender javaMailSender;
@Autowired
private VelocityEngine velocityEngine;
@RequestMapping("/testSendMail")
public String testSendMail() {
sendRegist("http://www.jiaobuchong.com/uuid/userId/1242425353");
return "ok";
}
private void sendRegist(String url) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
@Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "utf-8"); // 设置 utf-8防止出现乱码
message.setTo("ijiaobu@qq.com");
message.setFrom(new InternetAddress(portalEmailProperties.getFrom()));
message.setSubject("Registration Confirmation");
Map model = new HashMap<>();
model.put("url", url);
model.put("currentDate", "2016-03-31");
message.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "register.vm", "utf-8", model), true);
}
};
try {
this.javaMailSender.send(preparator);
} catch (MailException ex) {
ex.printStackTrace();
}
}
}
That's all, 玩去吧!!!
参考链接:
http://www.quweiji.com/%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6-spring-velocity/
http://andresmerida1.blogspot.com/2016/03/mailing-with-spring-framework-425.html
http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html
http://m.blog.csdn.net/article/details?id=46545491
网友评论