MailService { public void sendInlineResourceMail(String toMail,String subject,String content,List<InlineResource> resourceist) { MimeMessage message = sender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(formMail); helper.setCc(formMail); helper.setTo(toMail); helper.setSubject(subject); helper.setText(content, true); for (InlineResource inlineResource : resourceist) { FileSystemResource res = new FileSystemResource(new File(inlineResource.getPath())); helper.addInline(inlineResource.getCid(),res); } sender.send(message); logger.info("嵌入静态资源的邮件已经发送。"); } catch (MessagingException e) { logger.error("发送嵌入静态资源的邮件时发生异常！", e); e.printStackTrace(); }catch (Exception e) { logger.error("发送嵌入静态资源的邮件时发生异常！", e); e.printStackTrace(); } }  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 sendInlineResourceMail() { 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" + "<img src=\"cid:image1\"/> "+ "<img src=\"cid:image2\"/> "+ " <font color=\"red\">发送html</font>\r\n" + "</body>\r\n" + "</html>"; List<InlineResource> list = new ArrayList<InlineResource>(); String path = MailServiceTest.class.getClassLoader().getResource("image.jpg").getPath(); InlineResource resource = new InlineResource("image1",path); InlineResource resource2 = new InlineResource("image2",path); list.add(resource2); list.add(resource); mailService.sendInlineResourceMail("ljk163wy@163.com", "头像图片", html,list); }