Spring Boot發送郵件二——帶附件和內容有圖片的郵件

上一篇寫的是關於怎麼用Spring Boot發送一封只有文字的郵件,這一篇來說怎麼發送有附件的郵件和內容有圖的文件

搭建環境

1.發送帶附件的郵件

有了上一篇的基礎,帶附件的郵件直接上代碼

@SpringBootTest
class MailApplicationTests {
    @Autowired
    JavaMailSender javaMailSender;//配置好後,Spring Boot自動生成的郵件發送對象
    @Test
    public void test2() throws MessagingException {
        MimeMessage msg = javaMailSender.createMimeMessage();//創建模擬的消息
        MimeMessageHelper helper = new MimeMessageHelper(msg,true);//創建模擬的消息工具
        helper.setFrom("[email protected]");//發件人
        helper.setSentDate(new Date());//發送日期
        helper.setSubject("這是測試主題(帶附件)");//發送主題
        helper.setText("這是測試內容(帶附件)");//發送內容
        helper.setTo("[email protected]");//收件人
        helper.addAttachment("氣質美女.jpg",new File("C:\\Users\\Administrator\\Desktop\\氣質美女.jpg"));//添加附件,發送的是一張圖片
        javaMailSender.send(msg);//發送
    }
    }
  • 不能用SimpleMailMessage來處理附件了,Spring Boot 提供了一個模擬消息對象
    MimeMessage對象,它也是javaMailSender創建出來的。並進一步封裝成一個模擬消息工具MimeMessageHelper來處理要發送的信息
  • Attachment代表附件,Attach是黏貼的意思,Attachment黏貼的東西,就是附件的意思;源碼有兩個參數,一個是附件文件名稱,還有一個是File對象:
 public void addAttachment(String attachmentFilename, File file) throws MessagingException {
        Assert.notNull(file, "File must not be null");
        FileDataSource dataSource = new FileDataSource(file);
        dataSource.setFileTypeMap(this.getFileTypeMap());
        this.addAttachment(attachmentFilename, (DataSource)dataSource);
    }

運行 @Test 方法後登陸139郵箱發現剛纔發送的郵件
在這裏插入圖片描述
在線預覽一下,發送成功,感覺真不錯
在這裏插入圖片描述

2.如何發送內容帶圖片的郵件

  • 意思就是我如何一打開郵箱就能看見圖片,而不是在附件裏,還是直接上代碼:

@SpringBootTest
class MailApplicationTests {
    @Autowired
    JavaMailSender javaMailSender;

    @Test
    public void test3() throws MessagingException {
        MimeMessage msg = javaMailSender.createMimeMessage();//創建模擬的消息
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);//創建模擬的消息工具
        helper.setFrom("[email protected]");//發件人
        helper.setSentDate(new Date());//發送日期
        helper.setSubject("這是測試主題(帶圖片)");//發送主題
        helper.setText("這是測試內容(帶圖片)<br/>第一張圖片<div><img src='cid:p01'/></div>\n第二張圖片\n<div><img src='cid:p02'/></div>",true);//發送內容,第二個參數代表是否設置成html,true代表是
        helper.setTo("[email protected]");//收件人
        helper.addInline("p01", new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\氣質美女.jpg")));//在行裏P01的地方加入圖片
        helper.addInline("p02", new FileSystemResource(new File("C:\\Users\\Administrator\\Desktop\\氣質美女2.jpg")));//在行裏P02的地方加入
        javaMailSender.send(msg);
    }
  • 看這個代碼,是在 p01 或者 p02 地方替換成圖片,這點大家比較直觀的理解,那麼出現兩次 cid 是什麼意思呢,先別急,稍後再解答;
  • helper.setText("這是測試內容(帶圖片)<br/>第一張圖片<div><img src='cid:p01'/></div>\n第二張圖片\n<div><img src='cid:p02'/></div>",true);這個方法的第二個參數是布爾類型,代表是否要將文本處理成 HTML,它的源碼是
public void setText(String text, boolean html) throws MessagingException {
        Assert.notNull(text, "Text must not be null");
        Object partToUse;
        if (this.isMultipart()) {
            partToUse = this.getMainPart();
        } else {
            partToUse = this.mimeMessage;
        }

        if (html) {
            this.setHtmlTextToMimePart((MimePart)partToUse, text);
        } else {
            this.setPlainTextToMimePart((MimePart)partToUse, text);
        }

    }
  • 執行@Test方法發送郵件
  • 發送後打開郵箱:
    在這裏插入圖片描述
  • 帶圖片內容的郵件發送成功了,8行代碼就搞定了;接下來看剛纔的 cid問題,點擊“倒三角”後,點擊“顯示郵件原文”
    在這裏插入圖片描述
    在這裏插入圖片描述
  • 這張圖上 Content-ID: <p01> 代表的就是\<img src='cid:p01'/>片段,cid就是Content-ID的簡稱
  • 標紅框的上邊一行 Content-Disposition: inline 翻譯一下是內容的位置 冒號 在行裏
  • 標紅框的上邊兩行 Content-Transfer-Encoding: base64 翻譯一下是內容轉換編碼 冒號 base64(專門處理圖片)
  • 標紅框的上邊三行 Content-Type:image/jpeg 翻譯一下是內容類型 冒號 jpeg圖片
    這些內容就是 helper.setText() 和 helper.addInline() 兩個方法做的事,只不過 Java和郵箱處理成打開郵件時優雅的樣子

結束

好了,這篇帶附件和內容帶圖片的介紹就寫到這裏,又有了新問題,每次發郵件如果想改動內容就要動 Java 代碼,有沒有html一樣的模板呢,有的,下一篇介紹如何用Spring Boot 中Thymeleaf 做模板來發送郵件。
Spring Boot發送郵件三——Thymeleaf做模板的郵件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章