springboot仍然在狂速成长,才五个多月没有存眷,此刻看官网已经到1.5.3.RELEASE版本了。筹备逐步在写写springboot相关的文章,本篇文章利用springboot最新版本1.5.3举办开拓。
发送邮件应该是网站的必备成果之一,软件开发,什么注册验证,健忘暗码可能是给用户发送营销信息。最早期的时候我们会利用JavaMail相关api来写发送邮件的相关代码,厥后spring推出了JavaMailSender越发简化了邮件发送的进程,软件开发,在之后springboot对此举办了封装就有了此刻的spring-boot-starter-mail,本章文章的先容主要来自于此包。
简朴利用
1、pom包设置
pom包内里添加spring-boot-starter-mail包引用
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> </dependencies>
2、在application.properties中添加邮箱设置
spring.mail.host=smtp.qiye.163.com //邮箱处事器地点 spring.mail.username=xxx@oo.com //用户名 spring.mail.password=xxyyooo //暗码 spring.mail.default-encoding=UTF-8 mail.fromMail.addr=xxx@oo.com //以谁来发送邮件
3、编写mailService,这里只提出实现类。
@Component public class MailServiceImpl implements MailService{ private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender; @Value("${mail.fromMail.addr}") private String from; @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 { mailSender.send(message); logger.info("简朴邮件已经发送。"); } catch (Exception e) { logger.error("发送简朴邮件时产生异常!", e); } } }
4、编写test类举办测试
@RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { @Autowired private MailService MailService; @Test public void testSimpleMail() throws Exception { MailService.sendSimpleMail("ityouknow@126.com","test simple mail"," hello this is simple mail"); } }
至此一个简朴的文本发送就完成了。
加点料
可是在正常利用的进程中,我们凡是在邮件中插手图片可能附件来富厚邮件的内容,下面讲先容如何利用springboot来发送富厚的邮件。
发送html名目邮件
其它都稳定在MailService添加sendHtmlMail要领.
public void sendHtmlMail(String to, String subject, String content) { MimeMessage message = mailSender.createMimeMessage(); try { //true暗示需要建设一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); mailSender.send(message); logger.info("html邮件发送乐成"); } catch (MessagingException e) { logger.error("发送html邮件时产生异常!", e); } }
在测试类中构建html内容,测试发送
@Test public void testHtmlMail() throws Exception { String content="<html>\n" + "<body>\n" + " <p>hello world ! 这是一封Html邮件!</p>\n" + "</body>\n" + "</html>"; MailService.sendHtmlMail("ityouknow@126.com","test simple mail",content); }
发送带附件的邮件
在MailService添加sendAttachmentsMail要领.
public void sendAttachmentsMail(String to, String subject, String content, String filePath){ MimeMessage message = mailSender.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); mailSender.send(message); logger.info("带附件的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送带附件的邮件时产生异常!", e); } }
添加多个附件可以利用多条 helper.addAttachment(fileName, file)