spring-email/src/main/java/com/xiongyingqi/email/service/impl/EmailService.java
private Collection<Inline> handlerInlineImages(EmailVo emailVo, MimeMessageHelper msgHelper) throws MessagingException {
        Collection<Inline> inlines = null;
        if (emailVo.getInlineImageVos() != null && emailVo.getInlineImageVos().size() > 0) {
            inlines = new HashSet<Inline>();
            String html = emailVo.getHtml();
            if (html == null || html.trim().equals("")) {// 如果内容空或者不是 html则直接跳过
                return inlines;
            }

            //  ------------------------------------------- 查找带数字的标记 ${数字} -------------------------------------------
            Matcher matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX.matcher(html);

            int contentIdIndex = 0;
            while (matcherIndexTag.find()) {

                String htmlTag = matcherIndexTag.group(); // 查找到<img src="${1}" /> 的内容
                Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_INDEX.matcher(htmlTag);
                if (matcherDollar.find()) {

                    String dollarFind = matcherDollar.group();// 查找到${1}内容
                    String indexStr = dollarFind.replace("$", "").replace("{", "").replace("}", "").trim();// 移除外面的$符号和大括号
                    int index = Integer.parseInt(indexStr);
                    try {
                        InlineImageVo inlineImageVo = emailVo.getInlineImageVos().get(index);
                        File file = inlineImageVo.getFile();

                        String contentId = "file" + contentIdIndex++; // 生成contentId
//                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper

                        Inline inline = new Inline();
                        inline.setFile(file);
                        inline.setContentId(contentId);

                        inlines.add(inline);

                        htmlTag = matcherDollar.replaceAll("cid:" + contentId);
//                        System.out.println("indexStr ====== " + indexStr);
//                        System.out.println("htmlTag ====== " + htmlTag);

                        html = matcherIndexTag.replaceFirst(htmlTag);
                        matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX.matcher(html);

//                        System.out.println("html ====== " + html);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }

            //  ------------------------------------------- 查找带字母变量的标记 ${variable} -------------------------------------------
            Matcher matcherTag = PATTERN_FIND_TAG.matcher(html);
            while (matcherTag.find()) {
                String htmlTag = matcherTag.group(); // 查找到<img src="${1}" /> 的内容
                Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_SELF_DEFINE.matcher(htmlTag);
                if (matcherDollar.find()) {
                    String dollarFind = matcherDollar.group();// 查找到${1}内容
                    String dollarVariable = dollarFind.replace("$", "").replace("{", "").replace("}", "").trim();// 移除外面的$符号和大括号
                    try {
                        InlineImageVo inlineImageVo = null;
                        for (InlineImageVo imageVo : emailVo.getInlineImageVos()) {
                            if (imageVo.getContentId() != null && imageVo.getContentId().equals(dollarVariable)) {
                                inlineImageVo = imageVo;
                            }
                        }

                        if (inlineImageVo == null) {
                            continue;
                        }
                        File file = inlineImageVo.getFile();

//                        String contentId = "file" + contentIdIndex++; // 生成contentId
//                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper

                        Inline inline = new Inline();
                        inline.setFile(file);
                        inline.setContentId(inlineImageVo.getContentId());

                        inlines.add(inline);

                        htmlTag = matcherDollar.replaceAll("cid:" + inlineImageVo.getContentId());
//                        System.out.println("htmlTag ====== " + htmlTag);

                        html = matcherTag.replaceFirst(htmlTag);
                        matcherTag = PATTERN_FIND_TAG.matcher(html);

//                        System.out.println("html ====== " + html);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }


            emailVo.setHtml(html);

            if (inlines != null && inlines.size() > 0) {
                emailVo.setHtml(true);
            }
        }
        return inlines;
    }
private Collection<Inline> handlerInlineImages(EmailVo emailVo, MimeMessageHelper msgHelper) throws MessagingException {
        Collection<Inline> inlines = null;
        if (emailVo.getInlineImageVos() != null && emailVo.getInlineImageVos().size() > 0) {
            inlines = new HashSet<Inline>();
            String html = emailVo.getHtml();
            if (html == null || html.trim().equals("")) {// 如果内容空或者不是 html则直接跳过
                return inlines;
            }

            //  ------------------------------------------- 查找带数字的标记 ${数字} -------------------------------------------
            Matcher matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX.matcher(html);

            int contentIdIndex = 0;
            while (matcherIndexTag.find()) {

                String htmlTag = matcherIndexTag.group(); // 查找到<img src="${1}" /> 的内容
                Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_INDEX.matcher(htmlTag);
                if (matcherDollar.find()) {

                    String dollarFind = matcherDollar.group();// 查找到${1}内容
                    String indexStr = dollarFind.replace("$", "").replace("{", "").replace("}", "").trim();// 移除外面的$符号和大括号
                    int index = Integer.parseInt(indexStr);
                    try {
                        InlineImageVo inlineImageVo = emailVo.getInlineImageVos().get(index);
                        File file = inlineImageVo.getFile();

                        String contentId = "file" + contentIdIndex++; // 生成contentId
//                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper

                        Inline inline = new Inline();
                        inline.setFile(file);
                        inline.setContentId(contentId);

                        inlines.add(inline);

                        htmlTag = matcherDollar.replaceAll("cid:" + contentId);
//                        System.out.println("indexStr ====== " + indexStr);
//                        System.out.println("htmlTag ====== " + htmlTag);

                        html = matcherIndexTag.replaceFirst(htmlTag);
                        matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX.matcher(html);

//                        System.out.println("html ====== " + html);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }

            //  ------------------------------------------- 查找带字母变量的标记 ${variable} -------------------------------------------
            Matcher matcherTag = PATTERN_FIND_TAG.matcher(html);
            while (matcherTag.find()) {
                String htmlTag = matcherTag.group(); // 查找到<img src="${1}" /> 的内容
                Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_SELF_DEFINE.matcher(htmlTag);
                if (matcherDollar.find()) {
                    String dollarFind = matcherDollar.group();// 查找到${1}内容
                    String dollarVariable = dollarFind.replace("$", "").replace("{", "").replace("}", "").trim();// 移除外面的$符号和大括号
                    try {
                        InlineImageVo inlineImageVo = null;
                        for (InlineImageVo imageVo : emailVo.getInlineImageVos()) {
                            if (imageVo.getContentId() != null && imageVo.getContentId().equals(dollarVariable)) {
                                inlineImageVo = imageVo;
                            }
                        }

                        if (inlineImageVo == null) {
                            continue;
                        }
                        File file = inlineImageVo.getFile();

//                        String contentId = "file" + contentIdIndex++; // 生成contentId
//                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper

                        Inline inline = new Inline();
                        inline.setFile(file);
                        inline.setContentId(inlineImageVo.getContentId());

                        inlines.add(inline);

                        htmlTag = matcherDollar.replaceAll("cid:" + inlineImageVo.getContentId());
//                        System.out.println("htmlTag ====== " + htmlTag);

                        html = matcherTag.replaceFirst(htmlTag);
                        matcherTag = PATTERN_FIND_TAG.matcher(html);

//                        System.out.println("html ====== " + html);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }


            emailVo.setHtml(html);

            if (inlines != null && inlines.size() > 0) {
                emailVo.setHtml(true);
            }
        }
        return inlines;
    }
private Collection < Inline > handlerInlineImages ( EmailVo emailVo , MimeMessageHelper msgHelper ) throws MessagingException { Collection < Inline > inlines = null ; if ( emailVo . getInlineImageVos ( ) != null && emailVo . getInlineImageVos ( ) . size ( ) > 0 ) { inlines = new HashSet < Inline > ( ) ; String html = emailVo . getHtml ( ) ; if ( html == null || html . trim ( ) . equals ( "" ) ) { // 如果内容空或者不是 html则直接跳过 return inlines ; } //  ------------------------------------------- 查找带数字的标记 ${数字} ------------------------------------------- Matcher matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX . matcher ( html ) ; int contentIdIndex = 0 ; while ( matcherIndexTag . find ( ) ) { String htmlTag = matcherIndexTag . group ( ) ; // 查找到<img src="${1}" /> 的内容 Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_INDEX . matcher ( htmlTag ) ; if ( matcherDollar . find ( ) ) { String dollarFind = matcherDollar . group ( ) ; // 查找到${1}内容 String indexStr = dollarFind . replace ( "$" , "" ) . replace ( "{" , "" ) . replace ( "}" , "" ) . trim ( ) ; // 移除外面的$符号和大括号 int index = Integer . parseInt ( indexStr ) ; try { InlineImageVo inlineImageVo = emailVo . getInlineImageVos ( ) . get ( index ) ; File file = inlineImageVo . getFile ( ) ; String contentId = "file" + contentIdIndex ++ ; // 生成contentId //                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper Inline inline = new Inline ( ) ; inline . setFile ( file ) ; inline . setContentId ( contentId ) ; inlines . add ( inline ) ; htmlTag = matcherDollar . replaceAll ( "cid:" + contentId ) ; //                        System.out.println("indexStr ====== " + indexStr); //                        System.out.println("htmlTag ====== " + htmlTag); html = matcherIndexTag . replaceFirst ( htmlTag ) ; matcherIndexTag = PATTERN_FIND_TAG_AND_INDEX . matcher ( html ) ; //                        System.out.println("html ====== " + html); } catch ( Exception e ) { e . printStackTrace ( ) ; } } } //  ------------------------------------------- 查找带字母变量的标记 ${variable} ------------------------------------------- Matcher matcherTag = PATTERN_FIND_TAG . matcher ( html ) ; while ( matcherTag . find ( ) ) { String htmlTag = matcherTag . group ( ) ; // 查找到<img src="${1}" /> 的内容 Matcher matcherDollar = PATTERN_DOLLAR_VARIABLE_SELF_DEFINE . matcher ( htmlTag ) ; if ( matcherDollar . find ( ) ) { String dollarFind = matcherDollar . group ( ) ; // 查找到${1}内容 String dollarVariable = dollarFind . replace ( "$" , "" ) . replace ( "{" , "" ) . replace ( "}" , "" ) . trim ( ) ; // 移除外面的$符号和大括号 try { InlineImageVo inlineImageVo = null ; for ( InlineImageVo imageVo : emailVo . getInlineImageVos ( ) ) { if ( imageVo . getContentId ( ) != null && imageVo . getContentId ( ) . equals ( dollarVariable ) ) { inlineImageVo = imageVo ; } } if ( inlineImageVo == null ) { continue ; } File file = inlineImageVo . getFile ( ) ; //                        String contentId = "file" + contentIdIndex++; // 生成contentId //                        msgHelper.addInline(contentId, file);// 将附件内容传送给MimeMessageHelper Inline inline = new Inline ( ) ; inline . setFile ( file ) ; inline . setContentId ( inlineImageVo . getContentId ( ) ) ; inlines . add ( inline ) ; htmlTag = matcherDollar . replaceAll ( "cid:" + inlineImageVo . getContentId ( ) ) ; //                        System.out.println("htmlTag ====== " + htmlTag); html = matcherTag . replaceFirst ( htmlTag ) ; matcherTag = PATTERN_FIND_TAG . matcher ( html ) ; //                        System.out.println("html ====== " + html); } catch ( Exception e ) { e . printStackTrace ( ) ; } } } emailVo . setHtml ( html ) ; if ( inlines != null && inlines . size ( ) > 0 ) { emailVo . setHtml ( true ) ; } } return inlines ; }
处理内嵌对象

@param emailVo
@param msgHelper
@throws javax.mail.MessagingException
处理内嵌对象