Skip to main content link. Accesskey S
  • Help
  • HCL Logo
  • HCL Notes and Domino Application Development wiki
  • THIS WIKI IS READ-ONLY. Individual names altered for privacy purposes.
  • HCL Forums and Blogs
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • API Documentation
Search
Community Articles > Developing Applications > Developing XPage Applications > NotesMIMEEntity sample JavaScript code for XPages
  • Share Show Menu▼
  • Subscribe Show Menu▼

Recent articles by this author

NotesName sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesName. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesMIMEEntity sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesMIMEEntity. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntryCollection sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntryCollection. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewNavigator sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewNavigator. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntry sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntry. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
Community articleNotesMIMEEntity sample JavaScript code for XPages
Added by ~Elizabeth Umkroskiettu | Edited by ~Elizabeth Umkroskiettu on April 15, 2011 | Version 2
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
Here is sample JavaScript code for the class NotesMIMEEntity. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
ShowTable of Contents
HideTable of Contents
    • 0.1 BoundaryEnd, BoundaryStart, getFirstChildEntity, getNextSibling, getParentEntity
    • 0.2 Charset, ContentAsText, ContentSubType, ContentType, Encoding, Headers
    • 0.3 ContentAsText
    • 0.4 HeaderObjects
    • 0.5 Preamble
    • 0.6 createChildEntity, createHeader
    • 0.7 createParentEntity
    • 0.8 decodeContent, encodeContent
    • 0.9 getContentAsBytes
    • 0.10 getContentAsText
    • 0.11 getEntityAsText
    • 0.12 getNextEntity, getPrevSibling
    • 0.13 getNthHeader
    • 0.14 getPrevEntity
    • 0.15 getSomeHeaders
    • 0.16 remove
    • 0.17 setContentFromBytes
    • 0.18 setContentFromText

BoundaryEnd, BoundaryStart, getFirstChildEntity, getNextSibling, getParentEntity


This button displays the content of the current document if it is in MIME format.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	// If multipart MIME entity
	if (mime.getContentType().equals("multipart")) {
		// Print preamble
		if (!mime.getPreamble().equals("")) {
			requestScope.status = "Preamble:\t" + mime.getPreamble() + "\n";
		}
		// Print content of each child entity
		var child1:NotesMIMEEntity = mime.getFirstChildEntity();
		while (child1 != null) {
			requestScope.status += 
			child1.getBoundaryStart() + child1.getContentAsText() +
			child1.getBoundaryEnd() + "\n";
			var child2:NotesMIMEEntity = child1.getFirstChildEntity();
			if (child2 == null) {
				child2 = child1.getNextSibling();
				if (child2 == null) {
					child2 = child1.getParentEntity();
					if (child2 != null) {
						child2 = child2.getNextSibling();
					}
				}
			}
			child1 = child2;
		}
	}
	// If not multipart, just print content
	else {
		requestScope.status = mime.getContentAsText();
	}
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


Charset, ContentAsText, ContentSubType, ContentType, Encoding, Headers


This button gets the MIME content from the current document.

var doc:NotesDocument = currentDocument.getDocument();
var mime:NotesMIMEEntity = doc.getMIMEEntity();
if (mime != null) {
	var m:string = "Content type: " + mime.getContentType() + "\n" +
	"Content subtype: " + mime.getContentSubType() + "\n" +
	"Character set: " + mime.getCharset() + "\n" +
	"Encoding: " + mime.getEncoding();
	requestScope.status = doc.getItemValueString("Subject") + "\n" + m + "\n" +
	mime.getHeaders() + "\n" + mime.getContentAsText();
} else {
	requestScope.status = "Not MIME - " + doc.getItemValueString("Subject");
}


ContentAsText


This button sets the value of a rich text control (bound to requestScope.body) with the value of a rich text item (body) in the current document.

var doc:NotesDocument = currentDocument.getDocument();
if (doc.hasItem("body")) {
	var entity:NotesMIMEEntity = doc.getMIMEEntity("body");
	if (entity != null) {
		requestScope.body = entity.getContentAsText();
	}
}


HeaderObjects


This button gets all the headers for a document with MIME content.

var doc:NotesDocument = currentDocument.getDocument();
var mime:NotesMIMEEntity = doc.getMIMEEntity();
if (mime != null) {
	//requestScope.status = 	mime.getHeaders();
	var headers = mime.getHeaderObjects().iterator();
	while (headers.hasNext()) {
		var header:NotesMIMEHeader = headers.next();
		requestScope.status += "\n(" +
		header.getHeaderName() + ") " + header.getHeaderVal();
	}
} else {
	requestScope.status = "Not MIME - ";
}


Preamble


This button displays the content of the current document if it is in MIME format.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	// If multipart MIME entity
	if (mime.getContentType().equals("multipart")) {
		// Print preamble
		if (!mime.getPreamble().equals("")) {
			requestScope.status = "Preamble:\t" + mime.getPreamble() + "\n";
		}
		// Print content of each child entity
		var child1:NotesMIMEEntity = mime.getFirstChildEntity();
		while (child1 != null) {
			requestScope.status += 
			child1.getBoundaryStart() + child1.getContentAsText() +
			child1.getBoundaryEnd() + "\n";
			var child2:NotesMIMEEntity = child1.getFirstChildEntity();
			if (child2 == null) {
				child2 = child1.getNextSibling();
				if (child2 == null) {
					child2 = child1.getParentEntity();
					if (child2 != null) {
						child2 = child2.getNextSibling();
					}
				}
			}
			child1 = child2;
		}
	}
	// If not multipart, just print content
	else {
		requestScope.status = mime.getContentAsText();
	}
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


This button mails a document in MIME format.

var stream:NotesStream = session.createStream();
// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
// Create parent entity
doc.replaceItemValue("Form", "Memo");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Content-Type");
header.setHeaderVal("multipart/mixed");
header = body.createHeader("Subject");
header.setHeaderVal("MIME message");
header = body.createHeader("To");
header.setHeaderVal(requestScope.query);
// Create preamble
body.setPreamble("Preamble to multipart message.\n");
// Create first child entity
var child:NotesMIMEEntity = body.createChildEntity();
stream.writeText("Text of message for child 1.\n");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
stream.truncate();
// Create second child entity
child = body.createChildEntity();
stream.writeText("Text of message for child 2.\n");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
doc.send(false);
// Restore conversion
session.setConvertMIME(true);


createChildEntity, createHeader


This button creates a multipart MIME entity and sends it as a mail memo.

var stream:NotesStream = session.createStream();
// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
// Create parent entity
doc.replaceItemValue("Form", "Memo");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Content-Type");
header.setHeaderVal("multipart/mixed");
header = body.createHeader("Subject");
header.setHeaderVal("MIME message");
header = body.createHeader("To");
header.setHeaderVal(requestScope.query);
// Create first child entity
var child:NotesMIMEEntity = body.createChildEntity();
stream.writeText("Text of message for child 1.\n");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
stream.truncate();
// Create second child entity
child = body.createChildEntity();
stream.writeText("Text of message for child 2.");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
doc.send(false);
// Restore conversion
session.setConvertMIME(true);


This button creates a multipart MIME entity and sends it as a mail memo, as above, but positions the second child in front of the first.

var stream:NotesStream = session.createStream();
// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
// Create parent entity
doc.replaceItemValue("Form", "Memo");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Content-Type");
header.setHeaderVal("multipart/mixed");
header = body.createHeader("Subject");
header.setHeaderVal("MIME message");
header = body.createHeader("To");
header.setHeaderVal(requestScope.query);
// Create first child entity
var child:NotesMIMEEntity = body.createChildEntity();
stream.writeText("Text of message for child 1.\n");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
stream.truncate();
// Create second child entity
child = body.createChildEntity(child);
stream.writeText("Text of message for child 2.\n");
child.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
doc.send(false);
// Restore conversion
session.setConvertMIME(true);


createParentEntity


This button gets the MIME entity in the current document. If the entry is not multipart, the button creates a parent entity. The button then appends a child entity.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = currentDocument.getDocument();
var mime:NotesMIMEEntity = doc.getMIMEEntity();
if (mime != null) {
	// If multipart MIME entity
	if (!mime.getContentType().equals("multipart")) {
		parent = mime.createParentEntity();
	} else {
		parent = mime;
	}
	mime = parent.createChildEntity();
	var stream:NotesStream = session.createStream();
	stream.writeText("Additional text.\n\n");
	mime.setContentFromText(stream, "text/plain", NotesMIMEEntity.ENC_NONE);
	doc.save(true, true);
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


decodeContent, encodeContent


This button changes the encoding for a one-part MIME entity to None. If the entity content is already encoded (base64 or quoted-printable), the agent first decodes the entity.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = currentDocument.getDocument();
var mime:NotesMIMEEntity = doc.getMIMEEntity();
if (mime != null) {
	mime.decodeContent();
	mime.encodeContent(NotesMIMEEntity.ENC_NONE);
	doc.save(true, true);
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getContentAsBytes


This button gets the content of an image/gif MIME entity and saves it to a .gif file.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	if (mime.getContentType().equals("image") &&
	mime.getContentSubType().equals("gif")) {
		var stream:NotesStream = session.createStream();
		var pathname:string = "c:\\notes\\data\\temp.gif";
		if (stream.open(pathname, "binary")) {
			mime.getContentAsBytes(stream);
			stream.close();
		} else requestScope.status = "Can't open c:\\notes\\data\\temp.gif";
	} else System.out.println("Not GIF");
} else requestScope.status = "Not MIME";
// Restore conversion
session.setConvertMIME(true);


getContentAsText


This button displays the content of the current document if it is in text/plain MIME format.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	if (mime.getContentType().equals("text") &&
	mime.getContentSubType().equals("plain")) {
		var stream:NotesStream = session.createStream();
		mime.getContentAsText(stream);
		stream.setPosition(0); // must reset the stream position
		requestScope.status = stream.readText();
	}
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getEntityAsText


This button gets the headers and content of a MIME entity, including only the From and Subject headers.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	var filter = new java.util.Vector();
	filter.addElement("Subject");
	filter.addElement("From");
	var stream:NotesStream = session.createStream();
	mime.getEntityAsText(stream, filter, true);
	stream.setPosition(0); // must reset the stream position
	requestScope.status = stream.readText();
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getNextEntity, getPrevSibling


This button gets all the children at the end of the last branch of a multipart entity by searching breadth first.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	// Drill down to last child at bottom of first branch
	var child:NotesMIMEEntity = mime.getNextEntity(NotesMIMEEntity.SEARCH_BREADTH);
	while (child != null) {
		mime = child;
		child = mime.getNextEntity(NotesMIMEEntity.SEARCH_BREADTH);
	}
	// Get content of all children at bottom level
	// of first branch in reverse order
	while (mime != null) {
		requestScope.status += mime.getContentAsText() + "\n";
		mime = mime.getPrevSibling();
	}
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getNthHeader


This button gets all the entities from the end of the last branch of a multipart entity in reverse order.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	var header:NotesMIMEHeader = mime.getNthHeader("Subject");
	if (header != null) {
		requestScope.status = "Subject: " + header.getHeaderVal();
	}
		requestScope.status = "No Subject header";
	
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getPrevEntity


This button gets all the entities from the end of the last branch of a multipart entity in reverse order.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	// Drill down to last child at bottom of first branch
	var child:NotesMIMEEntity = mime.getNextEntity(NotesMIMEEntity.SEARCH_BREADTH);
	while (child != null) {
		mime = child;
		child = mime.getNextEntity(NotesMIMEEntity.SEARCH_BREADTH);
	}
	// Get content of all children at bottom level
	// of first branch in reverse order
	while (mime != null) {
		requestScope.status += mime.getContentAsText() + "\n";
		mime = mime.getPrevEntity(NotesMIMEEntity.SEARCH_BREADTH);
	}
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


getSomeHeaders


This button gets the Subject and From headers for a document with MIME content.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var mime:NotesMIMEEntity = currentDocument.getDocument().getMIMEEntity();
if (mime != null) {
	var filter = new java.util.Vector();
	filter.addElement("Subject");
	filter.addElement("From");
	requestScope.status = mime.getSomeHeaders(filter, true);
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


remove


This button removes the MIME entity or all MIME entities (in the case of a multipart MIME entity) from the current document or all selected documents.

// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = currentDocument.getDocument();
var mime:NotesMIMEEntity = doc.getMIMEEntity();
if (mime != null) {
	mime.remove();
	doc.save(true, true);
} else {
	requestScope.status = "Not MIME";
}
// Restore conversion
session.setConvertMIME(true);


setContentFromBytes


This button creates a document in MIME format. The MIME content is one .gif file.

var stream:NotesStream = session.createStream();
// Do not automatically convert MIME to rich text
session.setConvertMIME(false);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "main");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Subject");
header.setHeaderVal("MIME image from GIF file");
if (stream.open("c:\\notes\\data\\folder.gif", "binary")) {
	if (stream.getBytes() != 0) {
		body.setContentFromBytes(stream, "image/gif",
		NotesMIMEEntity.ENC_IDENTITY_BINARY);
	} else requestScope.status = "c:\\lotus\\notes\\data\\folder.gif has no content";
} else requestScope.status = "Error opening c:\\notes\\data\\folder.gif";
stream.close();
doc.save(true, true);
// Restore conversion
session.setConvertMIME(true);


setContentFromText


This button mails a document in MIME format. Also see send.

var stream:NotesStream = session.createStream();
session.setConvertMime(false); // do not convert mime to rich text
var doc:NotesDocument = database.createDocument();
doc.appendItemValue("Form", "Memo");
var body:NotesMIMEEntity = doc.createMIMEEntity();
var header:NotesMIMEHeader = body.createHeader("Subject");
if (!header.setHeaderVal("MIME message")) {
	requestScope.status = "Cannot set header val for Subject";
	return;
}
header = body.createHeader("To");
if (!header.setHeaderVal(requestScope.query)) { // address of mail recipient
	requestScope.status = "Cannot set header val for To";
	return;
}
stream.writeText("This is the text of the message.");
body.setContentFromText(stream, "text/plain;charset=UTF-8", NotesMIMEEntity.ENC_NONE);
doc.send();
session.setConvertMime(true); // restore coversion


The following button creates a document from content on the current XPage.

var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", requestScope.subject);
if (requestScope.body != null) {
	//requestScope.body is bound to an inputRichText control
	var body:com.ibm.xsp.http.MimeMultipart = requestScope.body;
	var stream:NotesStream = session.createStream();
	stream.writeText(body.getHTML());
	var entity:NotesMIMEEntity = doc.createMIMEEntity("body");
	entity.setContentFromText(stream,"text/html;charset=UTF-8", 1725);
	stream.close();
} else {
	requestScope.status = "no content";
}
doc.save();

  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (2)
collapsed Versions (2)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (2)Apr 15, 2011, 2:55:10 PM~Elizabeth Umkroskiettu  
1Mar 21, 2011, 8:26:06 PM~Ben Eljipypulettu  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL
  • Privacy
  • Accessibility