2016-03-03 111 views
2

我出口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添加的内容。我错过了什么来摆脱多余的线?

回答

1

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

您正在使用EOL_NONE,因此writeText方法未向文本中添加任何内容,但显然CR和LF在输出中都被视为新行。您可能想尝试使用带EOL_PLATFORM的out.writeText。

+0

out.append(notesStream.read());不需要EOL *参数,这是我的主要问题。 – stwissel

+0

我的意思是:getEntityAsText(out)不接受EOL *参数 - 并且有麻烦 – stwissel

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的,我需要的read()一个String,而是我需要readText()。现在在我的“项目城堡”中愉快地工作