Spring-Boot 邮件服务
本文记录学习的内容以及遇到的问题,本次使用的是Spring-Boot 的1.5.6版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
1.配置
- 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- application.properties配置
spring.mail.host=smtp.163.com #163邮箱的服务地址
spring.mail.username=xxxxx@163.com #邮箱账户
spring.mail.password=****** #邮箱授权码
spring.mail.default-encoding=UTF-8 #默认编码格式
mail.fromMail.addr=xxxxx@163.com #发送方邮箱号
需要注意的是需要邮箱开启smtp服务,需要登录邮箱到设置中取设置
2.创建接口
创建邮件发送服务接口
package com.suanmei.mail;
public interface MailService {
/**
* 发送简单邮件
* @param to 邮件目标地址
* @param subject 邮件标题
* @param context 邮件内容
*/
void sendSimpleMail(String to, String subject, String context);
/**
* 发送html邮件
* @param to 邮件目标地址
* @param subject 标题
* @param htmlpath html文件名称(需要放到类目录下)
*/
void sendHTMLMail(String to, String subject,String htmlpath);
/**
* 发送带附件邮件
* @param to 目标地址
* @param subject 标题
* @param context 主题内容
* @param filePath 文件地址
*/
void sendAttachmentsMail(String to, String subject, String context, String filePath);
}
3、简单邮件
实现一些简单的文本
- 邮件发送服务类实现
@Component
public class MailServiceImpl implements MailService{
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
@Autowired
private JavaMailSender mailSender; //注入邮件发送类
@Value("${mail.fromMail.addr}") //服务配置中的发送人
private String from;
@Override
public void sendSimpleMail(String to, String subject, String context) {
//创建简单邮件消息类
SimpleMailMessage message = new SimpleMailMessage();
//设置发送人
message.setFrom(from);
//设置发送目标
message.setTo(to);
//设置邮件标题
message.setSubject(subject);
//设置邮件内容
message.setText(context);
try {
//发送邮件
mailSender.send(message);
} catch (Exception e) {
log.error("发送邮件失败",e);
}
}
}
- 测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailSenderTest {
@Autowired
private MailService mailService;
@Test
public void simpleMailTest(){
mailService.sendSimpleMail("xxxxx@qq.com", "Test Simple Mail", "这是一封简单邮件的测试");
}
}
4、html邮件
发送html格式的文本
- 实现服务
@Override
public void sendHTMLMail(String to, String subject,String path) {
try {
//将html文本文件读取出来
URL utl = MailServiceImpl.class.getClassLoader().getResource(path);
File file = new File(utl.toURI());
Scanner sc = new Scanner(file);
StringBuffer sb = new StringBuffer();
while(sc.hasNext()){
sb.append(sc.nextLine());
}
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(sb.toString(),true);
log.info(sb.toString());
mailSender.send(message);
} catch (Exception e) {
log.error("发送失败!",e);
}
}
- 测试代码
@Test
public void htmlMailTest(){
mailService.sendHTMLMail("xxxx@qq.com", "Test Html Mail","email.html");
}
5、发送附件邮件
- 服务实现
@Override
public void sendAttachmentsMail(String to, String subject, String context, String filePath) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(context,true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String filename = filePath.substring(filePath.lastIndexOf(File.separator));
//添加附件,如果有多个附件就多次调用
helper.addAttachment(filename, file);
mailSender.send(message);
log.info("附件邮件已发送!");
} catch (MessagingException e) {
log.error("发送邮件失败",e);
}
}
- 测试代码
@Test
public void sendAttachmentsMailTest(){
mailService.sendAttachmentsMail("535860290@qq.com", "Test Attachments Mail","这是一封带附件的邮件","D://Java与模式.pdf");
}
6、测试当中出现的问题
- 报了一次无效用户,发现是发送方from未能从配置文件中读取出来
- 另一次就是未开启smtp服务,发送失败