环境
产品中经常用到密码修改、验证等发送邮箱
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件
#mail
spring.mail.host=smtp.163.com
spring.mail.username=znvrtest@163.com
spring.mail.password=znvr123
spring.mail.default-encoding=UTF-8
mail.fromMail.addr=znvrtest@163.com
代码
package com.mhm.mail;
/**
* Created by MaHuiming on 2018/11/5.
*/
public interface MailService {
/**
* 送文本文件
* @param to
* @param subject
* @param content
*/
void sendSimpleMail(String to, String subject, String content);
/**
* 发送网页邮件
* @param to
* @param subject
* @param content
*/
void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath);
/**
* 发送正文中有静态资源(图片)的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId);
}
实现类
package com.mhm.mail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* Created by MHm on 2018/11/5.
*/
@Component
@Slf4j
public class MailServiceImpl implements MailService{
@Autowired
private JavaMailSender javaMailSender;
@Value("${mail.fromMail.addr}")
private String from;
/**
* 发送文本文件
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
javaMailSender.send(message);
} catch (Exception e) {
log.error("send simple Mail error:{}",e);
}
}
/**
* 发送网页邮件
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("send html Mail error:{}",e);
}
}
/**
* 发送带附件邮件
* @param to
* @param subject
* @param content
* @param filePath
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("send Attachments Mail error:{}",e);
}
}
/**
* 发送正文中有静态资源(图片)的邮件
* @param to
* @param subject
* @param content
* @param rscPath
* @param rscId
*/
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
javaMailSender.send(message);
} catch (MessagingException e) {
log.error("send resources Mail error:{}",e);
}
}
}
发送邮件controller
package com.mhm.controller;
import com.mhm.mail.MailService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* 测试发送邮件
* Created by MHM on 2018/11/5.
*/
@RestController
public class MailController {
@Resource
private MailService mailService;
@RequestMapping(value = "/sendMail")
public String sendMail(@RequestParam(value = "mailAddress", required = true) String mailAddress,@RequestParam(value = "title", required = true) String title,
@RequestParam(value = "content", required = true) String content){
mailService.sendSimpleMail(mailAddress,title,content);
return "success";
}
}