MailService { public void sendAttachmentsMail(String toMail,String subject,String content,String filePath) { MimeMessage message = sender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(formMail); helper.setTo(toMail); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf("/")); helper.addAttachment(fileName, file); sender.send(message); logger.info("发送给"+toMail+"带附件的邮件已经发送。"); } catch (MessagingException e) { e.printStackTrace(); logger.error("发送给"+toMail+"带附件的邮件时发生异常！", e); } }  void sendSimpleMail(String toMail,String subject,String content); void sendHtmlMail(String toMail,String subject,String content); void sendInlineResourceMail(String toMail,String subject,String content,List<InlineResource> resourceist); void sendInlineResourceMailForGroupSending(String[] toMail,String subject,String content,List<InlineResource> resourceist); void sendAttachmentsMail(String toMail,String subject,String content,String filePath);  }
@Test public void sendAttachmentsMail() { String html= "<!DOCTYPE html>\r\n" + "<html>\r\n" + "<head>\r\n" + "<meta charset=\"UTF-8\">\r\n" + "<title>Insert title here</title>\r\n" + "</head>\r\n" + "<body>\r\n" + " <font color=\"red\">发送html</font>\r\n" + "</body>\r\n" + "</html>"; String path = MailServiceTest.class.getClassLoader().getResource("image.jpg").getPath(); mailService.sendAttachmentsMail("1165904938@qq.com", "这是一个测试邮件", html, path); }