Andy Brunner
Andy Brunner

Reputation: 173

How to copy all the content of an Internet mime message to a body item in Notes?

I need to copy the entire body content of an Internet mail to a new Notes document.

Object internetMsgBody = internetMsg.getContent();

MIMEEntity notesBodyItem = notesDocument.createMIMEEntity("Body");
Stream mimeStream = dominoSession.createStream();
...?...
notesBodyItem.setContentFromBytes(mimeStream, msgContentType, 
MIMEEntity.ENC_NONE);

The internetMsgBody can be String, MimeMultiPart or InputStream (according to the documentation). I know how I can handle String :) but for the other Object types, I need some help. There is no need for any MIME or Parts content type handling.

Thanks!

Upvotes: 0

Views: 1290

Answers (3)

Andy Brunner
Andy Brunner

Reputation: 173

Just to follow up on this problem:

I came up with a much easier and more elegant solution. There is a writeTo() method which streams-out all the multipart data. This can then be streamed in again to a Domino stream which fills in the MIMEEntry body item.

case "javax.mail.internet.MimeMultipart": {

    // Create input stream with content of MIME data
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    message.writeTo(outputStream);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    outputStream.close();

    // Create Domino stream and fill it with the MIME data
    Stream dominoStream = dbGetSession().createStream();

    byte[] buffer = new byte[4096];
    int lengthTotal = 0;
    int length      = 0;

    while (true) {
        length = inputStream.read(buffer);
        if (length < 1)
            break;
        lengthTotal += length;
        dominoStream.write(buffer);
    }

    inputStream.close();

    // Create Domino MIME "Body" item with content of MIME data
    MIMEEntity dominoMIMEItem = mailDocument.createMIMEEntity("Body");
    dominoMIMEItem.setContentFromBytes(dominoStream, "", MIMEEntity.ENC_NONE);
}

Upvotes: 0

Andy Brunner
Andy Brunner

Reputation: 173

Thanks Dave, you pointed me to the missing part. My solution (so far) is to create a parent Domino MIMEEntity which holds all MIME Parts as children.

Code excerpt:

MimeMultipart mimeMultiparts = (MimeMultipart) message.getContent();
int partCount = mimeMultiparts.getCount();
MIMEEntity dominoParentItem = mailDocument.createMIMEEntity("Body");
Stream dominoStream = dbGetSession().createStream();    

for (int counter = 0; counter < partCount; counter++) {
  MimeBodyPart mimeBodyPart = (MimeBodyPart) mimeMultiparts.getBodyPart(counter);
  MIMEEntity dominoChildItem = dominoParentItem.createChildEntity();

  InputStream input = mimeBodyPart.getRawInputStream();

  byte[]    buffer      = new byte[4096];
  int       lengthTotal = 0;
  int       length      = 0;

  while (true) {
    length = input.read(buffer);
    if (length < 1)
      break;
    lengthTotal += length;
    dominoStream.write(buffer);
  }

  String encodingType   = mimeBodyPart.getEncoding();
  int dominoEncoding    = MIMEEntity.ENC_NONE;

  if (encodingType != null) {
    if (encodingType.toLowerCase().contains("base64"))
      dominoEncoding = MIMEEntity.ENC_BASE64;
    if (encodingType.toLowerCase().contains("7bit"))
       dominoEncoding = MIMEEntity.ENC_IDENTITY_7BIT;
    if (encodingType.toLowerCase().contains("8bit"))
       dominoEncoding = MIMEEntity.ENC_IDENTITY_8BIT;
    if (encodingType.toLowerCase().contains("binary"))
       dominoEncoding = MIMEEntity.ENC_IDENTITY_BINARY;
    if (encodingType.toLowerCase().contains("quoted-printable"))
       dominoEncoding = MIMEEntity.ENC_QUOTED_PRINTABLE;

    if (dominoEncoding == MIMEEntity.ENC_NONE)
       dominoEncoding = MIMEEntity.ENC_EXTENSION;
  }

  dominoChildItem.setContentFromBytes(dominoStream, mimeBodyPart.getContentType(), dominoEncoding);

Upvotes: 1

Dave Delay
Dave Delay

Reputation: 1320

In Notes, a multi-part MIME message is represented as a set of items of the same name (usually Body) each of which is TYPE_MIME. Think of a MIMEEntity instance as corresponding to a single one of these items. In other words, a MIMEEntity represents a single MIME part.

So if your input is a multi-part MIME message, you may have to parse the message into individual parts and create a MIMEEntity for each. Unfortunately, the Java back-end classes don't include a MIME parser. Of course, your question states that internetMsg.getContent() may return a MimeMultipart. If that's the case, it sounds like the MIME is already parsed for you.

Either way -- whether your input is a stream of many parts or a MimeMultipart -- I suggest you look at MimeMessageParser.java from the XPages Extension Library. It uses mime4j to parse an input stream. If there are multiple parts in the stream, it uses MIMEEntity to write each part as a separate item. Although the use of mime4j doesn't sound relevant, you may find some useful hints in that code. It implements a very similar use case.

Upvotes: 1

Related Questions