1.內嵌一張圖片:html
public void sendSingleImageEmail(){ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8"); helper.setTo("gui@163.com"); helper.setFrom("chen@163.com"); helper.setSubject("每日數據統計分析");//郵件主題 //郵件內容 helper.setText("<html><head></head><body><h1>hello</h1><img src='cid:picture'/></body></html>",true); FileSystemResource img = new FileSystemResource(new File("C:/single.jpg")); helper.addInline("picture", img);//'picture'爲img標籤的cid //發送郵件 mailSender.send(msg); }
2.內嵌多張圖片:java
public void sendMultipleImageEmail(){ MimeMessage msg = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8"); helper.setTo("gui@163.com"); helper.setFrom("chen@163.com"); helper.setSubject("每日數據統計分析");//郵件主題 //郵件內容(內嵌兩張圖片) helper.setText( "<html><head></head><body><h1>hello</h1><img src='cid:picture1'/><h1>hello another</h1><img src='cid:picture2'/></body></html>", true); //第一張圖片 FileSystemResource img1 = new FileSystemResource(new File("C:/multiple.jpg")); helper.addInline("picture1", img1); //第二張圖片 FileSystemResource img2 = new FileSystemResource(new File("C:/multiple2.jpg")); helper.addInline("picture2", img2); //發送郵件 mailSender.send(msg); }