Create email message with a specific sender under IBM Domino

Normally, when an agent sends a message under Domino, the sender field is set to the signer of the Domino agent. There are tons of articles in the Internet about this topic, but I could not find a complete solution.

When you issue Document.send(), the “From” field of the email message is overwritten with the agent signer. But if you use Document.save(), all fields are left untouched. Here is a sample code snippet  to send a message:

public void sendMessage() throws Exception {

	// Variables
	lotus.domino.Session		notesSession	= null;
	lotus.domino.Database		notesDatabase	= null;
	lotus.domino.Document		notesDocument	= null;
	lotus.domino.RichTextItem	notesRTItem		= null;
	lotus.domino.DateTime		notesDateTime	= null;
	String						emailSender		= "administrator@acme.com";
	String						emailRecipient	= "john.doe@acme.com";
	String						emailSubject	= "Message from the Domino server";
	String						emailBodyText	= "This is a sample body text";

	try {
		notesSession = NotesFactory.createSession();
		notesDatabase = notesSession.getDatabase(null, "mail.box");

		if (!notesDatabase.isOpen())
			throw new Exception("Unable to open the Domino router mail box");

		notesDocument = notesDatabase.createDocument();
		notesDocument.replaceItemValue("Form", "Memo");
		notesDocument.replaceItemValue("From", emailSender);
		notesDocument.replaceItemValue("SMTPOriginator", emailSender);
		notesDocument.replaceItemValue("Sender", emailSender);
		notesDocument.replaceItemValue("INetFrom", emailSender);
		notesDocument.replaceItemValue("Principal", emailSender + '@' + notesSession.getEnvironmentString("Domain", true));
		notesDocument.replaceItemValue("SendTo", emailRecipient);
		notesDocument.replaceItemValue("Subject", emailSubject);

		notesDateTime = getDominoSession().createDateTime("Today");
		notesDateTime.setNow();
		notesDocument.replaceItemValue("PostedDate", notesDateTime);

		notesRTItem = notesDocument.createRichTextItem("Body");
		notesRTItem.appendText(emailBodyText);

		notesDocument.save(true, false);

		} catch (NotesException e) {
			throw new Exception("Unable to send the message: " + e.text);
		} finally {
			try {
				if (notesDateTime != null)
					notesDateTime.recycle();

				if (notesRTItem != null)
					notesRTItem.recycle();

				if (notesDocument != null)
					notesDocument.recycle();

				if (notesDatabase != null)
					notesDatabase.recycle();

				if (notesSession != null)
					notesSession.recycle();
			} catch (NotesException e) {
				// Ignoring any object cleanup errors
			}
		}
	}
}