如何避免MIME导出中的额外换行?

时间:2016-03-03 20:46:32

标签: xpages lotus-notes mime

我使用以下代码导出MIME电子邮件:

    public String fromRawMime(final Session s, final Document doc) throws NotesException {
        final Stream notesStream = s.createStream();
        final MIMEEntity rootMime = doc.getMIMEEntity();

        // check if it is multi-part or single
        if (rootMime.getContentType().equals("multipart")) {
            this.printMIME(rootMime, notesStream);
        } else {
            // We can just write the content into the
            // Notes stream to get the bytes
            rootMime.getEntityAsText(notesStream);
        }

        // Write it out
        notesStream.setPosition(0);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.append(notesStream.read());
        notesStream.close();

        notesStream.recycle();
        rootMime.recycle();

        return out.toString();
    }

    // Write out a mime entry to a Stream object, includes sub entries
    private void printMIME(final MIMEEntity mimeRoot, final Stream out) throws NotesException {
        if (mimeRoot == null) {
            return;
        }

        // Encode binary as base64
        if (mimeRoot.getEncoding() == MIMEEntity.ENC_IDENTITY_BINARY) {
            mimeRoot.decodeContent();
            mimeRoot.encodeContent(MIMEEntity.ENC_BASE64);
        }

        out.writeText(mimeRoot.getBoundaryStart(), Stream.EOL_NONE);
        mimeRoot.getEntityAsText(out);
        out.writeText(mimeRoot.getBoundaryEnd(), Stream.EOL_NONE);

        if (mimeRoot.getContentType().equalsIgnoreCase("multipart")) {
            // Print preamble if it isn't empty
            final String preamble = mimeRoot.getPreamble();
            if (!preamble.isEmpty()) {
                out.writeText(preamble, Stream.EOL_NONE);
            }

            // Print content of each child entity - recursive calls
            // Include recycle of mime elements
            MIMEEntity mimeChild = mimeRoot.getFirstChildEntity();
            while (mimeChild != null) {
                this.printMIME(mimeChild, out);
                final MIMEEntity mimeNext = mimeChild.getNextSibling();
                // Recycle to ensure we don't bleed memory
                mimeChild.recyle();
                mimeChild = mimeNext;
            }
        }
    }

结果每行包含一个空行。包括使用getEntityAsText添加的内容。为了摆脱多余的线路,我错过了什么?

2 个答案:

答案 0 :(得分:1)

电子邮件RFC要求使用CRLF来终止文本行。

您正在使用EOL_NONE,因此writeText方法不会向文本添加任何内容,但显然CR和LF都被视为输出中的换行符。您可能希望尝试将out.writeText与EOL_PLATFORM一起使用。

答案 1 :(得分:0)

魔鬼在细节......

printMIME函数运行正常。改变EOL没有影响。但是我稍后添加了EOL_PLATFORM以获得最终结果,以便将标题与内容分开。

违规代码是:

    notesStream.setPosition(0);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    out.append(notesStream.read());
    notesStream.close();

原来,它似乎将MIME中的任何内容解释为2行换行。所以代码需要改为:

    notesStream.setPosition(0);
    String out  = notesStream.readText();
    notesStream.close();

所以我需要OutputStream而不是String,而不是read()而不是readText()。现在在我的“项目城堡”中愉快地工作