class 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);// 邮件抄送给自己防止发送不成功！com.sun.mail.smtp.SMTPSendFailedException: 554 DT:SPM 126 smtp9
			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();
		}
	}

}

class MailServiceTest {

	@Autowired
	private MailService mailService;

	@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);
		//mailService.sendInlineResourceMail("******@163.com", "这是一个测试邮件", html,list);
	}

}