君子坐而论道,少年起而行之
前提
写一篇简单的邮件发送功能。
环境
1.SpringBoot1.5.x
2.JDK8
3.Maven
4.Lombok插件
5.email依赖
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.1</version>
</dependency>
实现
实体类email
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(toBuilder = true, builderClassName = "Builder")
public class Email {
/**
* 邮件主题
*/
private String subject;
/**
* 文本内容
*/
private String text;
/**
* 是否开启html方式
*/
private boolean htmlText;
/**
* 发件人
*/
private String form;
/**
* 收件人
*/
@lombok.Builder.Default
private List<String> tos = new ArrayList<>(0);
/**
* 抄送人
*/
@lombok.Builder.Default
private List<String> ccs = new ArrayList<>(0);
/**
* 密送人
*/
@lombok.Builder.Default
private List<String> bcc = new ArrayList<>(0);
/**
* 附件
*/
@lombok.Builder.Default
private Map<String, File> attachments = new HashMap<>(0);
}
通用类
自定义通用类实现email发送。
@Autowired
private JavaMailSender javaMailSender;
private String name;
@Override
public void sendMimeMessage(Email email) throws MessagingException {
javaMailSender.send(buildMimeMessage(email));
}
//赋值信息
private MimeMessage buildMimeMessage(Email email) throws MessagingException {
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true, "GBK");
try {
//邮件主题
mimeMessageHelper.setSubject(email.getSubject());
//邮件发送人
mimeMessageHelper.setFrom(email.getForm());
//邮件收信人
mimeMessageHelper.setTo(email.getTos().toArray(new String[0]));
//抄送人
mimeMessageHelper.setCc(email.getCcs().toArray(new String[0]));
//密送人
mimeMessageHelper.setBcc(email.getBccs().toArray(new String[0]));
//邮件内容
mimeMessageHelper.setText(email.getText(), email.isHtmlText());
for (Map.Entry<String, File> attachment : email.getAttachments().entrySet()) {
//
mimeMessageHelper.addAttachment(attachment.getKey(), attachment.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
return mimeMessageHelper.getMimeMessage();
}
配置MailProperties类
这个是Mail的配置文件,配置文件以spring.mail开头并且附带上该类中的属性名称即可完成赋值。
package org.springframework.boot.autoconfigure.mail;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for email support.
*
* @author Oliver Gierke
* @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.2.0
*/
@ConfigurationProperties(prefix = "spring.mail")
public class MailProperties {
private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
/**
* SMTP server host. For instance, `smtp.example.com`.
*/
private String host;
/**
* SMTP server port.
*/
private Integer port;
/**
* Login user of the SMTP server.
*/
private String username;
/**
* Login password of the SMTP server.
*/
private String password;
/**
* Protocol used by the SMTP server.
*/
private String protocol = "smtp";
/**
* Default MimeMessage encoding.
*/
private Charset defaultEncoding = DEFAULT_CHARSET;
/**
* Additional JavaMail Session properties.
*/
private Map<String, String> properties = new HashMap<>();
/**
* Session JNDI name. When set, takes precedence over other Session settings.
*/
private String jndiName;
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getProtocol() {
return this.protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
public Charset getDefaultEncoding() {
return this.defaultEncoding;
}
public void setDefaultEncoding(Charset defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}
public Map<String, String> getProperties() {
return this.properties;
}
public void setJndiName(String jndiName) {
this.jndiName = jndiName;
}
public String getJndiName() {
return this.jndiName;
}
}
配置方式一:
yaml方式
mail:
host: #邮件地址
username: #用户名
password: #密码
properties:
mail:
smtp:
auth: true #stmp认证
timeout: 25000 #超时
配置方式二:
properties方式
//注入资源
@PropertySource(value = {"classpath:emailproperties.properties"})
spring.mail.host= #邮件地址
spring.mail.username= #用户名
spring.mail.password= #密码
spring.mail.properties.smtp.auth= #stmp认证
spring.mail.properties.smtp.timeout= #超时
测试请求
@GetMapping(value = "/sendmail")
@ResponseBody
public String sendMail() throws MessagingException {
Email email = new Email();
List<String> listTos = new ArrayList<String>(); //收件人
listTos.add("xxxx");
email.setText("<div style='background:#256;height:10%'></div><div><h3>Dear Admin:</div> <div style='color:red;font-size:15px;'> 收到邮件</nbsp;></div><div style='background:#256;height:10%'></div>");
email.setSubject("邮件接收");
email.setHtmlText(true);
email.setTos(listTos);
EmailUtil.sendMimeMessage(email);
return "success";
}
效果图
已完成。