Skip to content

OKMMail

In almost all methods you’ll see a parameter named “mailId”. The value of this parameter can be a valid mail UUID.

Regarding the parameter named “fldId”, the value of this parameter can be a valid folder UUID or record node.

Also, in all methods you’ll see a parameter named “token”. Because the Cron Task is executed in the background without authentication, the methods used in this scenario might use the token parameter. From the default application execution context you must use the “null” value, which indicates that the application must use the “user session”.

Description:

Method Return values Description
create(String token, Mail mail, InputStream is) Mail Create a new mail node from a Mail bean and an input stream.
  • The mail bean must have the path set to a valid folder or record path where the mail will be stored.
  • The is parameter is the binary content of the mail (EML or MSG format).

Example:

package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
InputStream is = null;
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
is = new FileInputStream("/home/openkm/message.eml");
Mail mail = new Mail();
mail.setPath("/okm:root/test/message.eml");
Mail newMail = okmMail.create(null, mail, is);
System.out.println(newMail);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}
}

Description:

Method Return values Description
getProperties(String token, String mailId) Mail Returns the mail properties.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
Mail mail = okmMail.getProperties(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
createAttachment(String token, String mailId, String docName, InputStream is) Document Add an attachment to the mail.

Example:

package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
InputStream is = null;
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
is = new FileInputStream("/home/openkm/sample.pdf");
Document doc = okmMail.createAttachment(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "sample.pdf", is);
System.out.println(doc);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}
}

Description:

Method Return values Description
deleteAttachment(String token, String mailId, String docId) void Delete an attachment from the mail.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.deleteAttachment(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "c98d704f-c9f2-4f75-9923-878300b36b52");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getAttachments(String token, String mailId) List Returns a list of all attachments for a mail.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.Document;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
for (Document doc : okmMail.getAttachments(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75")) {
System.out.println(doc);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
delete(String token, String mailId) void Delete a mail.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.delete(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
purge(String token, String mailId) void The mail is permanently removed from the repository.
  • Usually you will purge mails into /okm:trash/userId - the personal trash user location - but it is possible to directly purge any mail from the whole repository.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.purge(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
rename(String token, String mailId, String newName) Mail Rename a mail.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.rename(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "newname");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
move(String token, String mailId, String dstId) void Moves a mail into some folder or record.
  • The values of the dstId parameter should be a folder or record UUID.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.move(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "3636f3bb-779c-4851-b145-0d53e0702b0f");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
copy(String token, String mailId, String dstId) Mail Copies a mail into a folder or record.
  • The values of the dstId parameter should be a folder or record UUID.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.copy(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "3636f3bb-779c-4851-b145-0d53e0702b0f");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
copy(String token, String mailId, String dstId, String newName) Mail Copies a mail into a folder or record.
  • The values of the dstId parameter should be a folder or record UUID.
  • When the newName parameter value is null, the mail will preserve the same name.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.copy(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "3636f3bb-779c-4851-b145-0d53e0702b0f", "newname");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
extendedCopy(String token, String mailId, String dstId, ExtendedAttributes extAttr) Mail Copies a mail with the associated data into some folder or record.
  • The values of the dstId parameter should be a folder or record UUID.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.ExtendedAttributes;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
ExtendedAttributes extAttr = new ExtendedAttributes();
extAttr.setNotes(true);
extAttr.setKeywords(true);
extAttr.setCategories(true);
extAttr.setPropertyGroups(true);
okmMail.extendedCopy(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "3636f3bb-779c-4851-b145-0d53e0702b0f", extAttr);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
extendedCopy(String token, String mailId, String dstId, ExtendedAttributes extAttr, String newName) Mail Copies a mail with the associated data into some folder or record.
  • The values of the dstId parameter should be a folder or record UUID.
  • When the newName parameter value is null, the mail will preserve the same name.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.ExtendedAttributes;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
ExtendedAttributes extAttr = new ExtendedAttributes();
extAttr.setNotes(true);
extAttr.setKeywords(true);
extAttr.setCategories(true);
extAttr.setPropertyGroups(true);
okmMail.extendedCopy(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "3636f3bb-779c-4851-b145-0d53e0702b0f", extAttr, "new_name");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getChildren(String token, String fldId) List Returns a list of all mails whose parent is fldId.
  • The parameter fldId can be a folder or a record node.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
for (Mail mail : okmMail.getChildren(null, "3636f3bb-779c-4851-b145-0d53e0702b0f")) {
System.out.println(mail);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
isValid(String token, String mailId) boolean Returns a boolean indicating whether the node is a mail or not.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
boolean valid = okmMail.isValid(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
System.out.println(valid);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getPath(String token, String uuid) String Converts a mail UUID to a mail path.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
String path = okmMail.getPath(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
sendMail(String token, List recipients, String subject, String body) void Sends a mail. Deprecated

Example:

package com.openkm;
import java.util.ArrayList;
import java.util.List;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
StringBuffer body = new StringBuffer();
body.append("Test message body.");
List<String> recipients = new ArrayList<>();
recipients.add("gnu.java.sergio@gmail.com");
okmMail.sendMail(null, recipients, "Testing sending mail from OpenKM", body.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
sendMail(String token, String from, List recipients, String subject, String body) void Sends a mail specifying the sender address.
  • Use this overload to specify a custom from address for the outgoing mail.

Example:

package com.openkm;
import java.util.ArrayList;
import java.util.List;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
StringBuffer body = new StringBuffer();
body.append("Test message body.");
List<String> recipients = new ArrayList<>();
recipients.add("user@example.com");
okmMail.sendMail(null, "sender@example.com", recipients, "Testing sending mail from OpenKM", body.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setTitle(String token, String mailId, String title) void Sets the mail title.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.setTitle(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "new title");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
sendMailWithAttachments(String token, List toRecipients, List ccRecipients, List bccRecipients,
List replyToMails, String subject, String body, List docsId, String dstId)
Mail Sends a mail message with attachment.
  • The values of the dstId parameter should be a folder or record UUID. The dstId parameter indicates where the mail will be stored in the repository after it is sent.
  • Other parameters:

Example:

package com.openkm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
List<String> toRecipients = Arrays.asList("gnujavasergio@mail.com");
List<String> ccRecipients = new ArrayList<>();
List<String> bccRecipients = new ArrayList<>();
List<String> replyToMails = new ArrayList<>();
List<String> docsId = Arrays.asList("b153c4b7-3d1c-4589-bd42-0ed0f34fd338", "d168924d-e801-426d-a222-00124a1461bd");
Mail mail = okmMail.sendMailWithAttachments(null, toRecipients, ccRecipients, bccRecipients, replyToMails,
"some subject", "some body", docsId, "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474");
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
sendMailWithAttachments(String token, String from, List toRecipients, List ccRecipients, List bccRecipients,
List replyToMails, String subject, String body, List docsId, String dstId)
Mail Sends a mail message with attachment, specifying the sender address.
  • The from parameter sets a custom sender address for the outgoing mail.
  • The values of the dstId parameter should be a folder or record UUID. The dstId parameter indicates where the mail will be stored in the repository after it is sent.
  • Other parameters:

Example:

package com.openkm;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
String from = "sender@example.com";
List<String> toRecipients = Arrays.asList("gnujavasergio@mail.com");
List<String> ccRecipients = new ArrayList<>();
List<String> bccRecipients = new ArrayList<>();
List<String> replyToMails = new ArrayList<>();
List<String> docsId = Arrays.asList("b153c4b7-3d1c-4589-bd42-0ed0f34fd338");
Mail mail = okmMail.sendMailWithAttachments(null, from, toRecipients, ccRecipients, bccRecipients, replyToMails,
"some subject", "some body", docsId, "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474");
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
importEml(String token, String fldId, String title, InputStream is) Mail Import a mail in EML format.
  • The values of the fldId parameter should be a folder or record UUID. The fldId parameter indicates where the mail will be stored in the repository after it is imported.

Example:

package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
InputStream is = null;
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
is = new FileInputStream("/home/openkm/sample1.eml");
Mail mail = okmMail.importEml(null, "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474", "some title", is);
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}
}

Description:

Method Return values Description
importMsg(String token, String fldId, String title, InputStream is) Mail Import a mail in MSG format.
  • The values of the fldId parameter should be a folder or record UUID. The fldId parameter indicates where the mail will be stored in the repository after it is imported.

Example:

package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
InputStream is = null;
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
is = new FileInputStream("/home/openkm/test subject.msg");
Mail mail = okmMail.importMsg(null, "b67fe3ae-bcca-4947-b8ea-0cc7aedd4474", "some title", is);
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
}
}

Description:

Method Return values Description
setNodeClass(String token, String uuid, long ncId) void Set the NodeClass.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
long ncId = 2;
okmMail.setNodeClass(null, "7ce1b4a8-4ade-4dce-8d7d-4e99a6cd368b", ncId);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setDispositionStage(String token, String uuid, long stage) void Set the disposition stage

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
long stage = 1;
okmMail.setDispositionStage(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", stage);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
setDescription(String token, String uuid, String description) void Set the description.

Example:

package com.openkm;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
okmMail.setDescription(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75", "some description");
Mail mail = okmMail.getProperties(null, "0747c0cb-5e4a-4088-92a9-ab97b401af75");
System.out.println(mail);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getContent(String token, String mailId) InputStream Retrieves mail content (binary data).

Example:

package com.openkm;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
OutputStream fos = new FileOutputStream("/home/openkm/test.eml");
InputStream is = okmMail.getContent(null, "b405a504-d8cb-4166-ac51-22f68acee8c5");
IOUtils.copy(is, fos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(fos);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
createWizard(String token, String path, String title, InputStream is, String type) WizardNode Create a new email with a wizard.
  • The parameters path should be any valid folder or record PATH.
  • Available types:

Example:

package com.openkm;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.bean.WizardNode;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
InputStream is = new FileInputStream("/home/openkm/sample2.eml");
WizardNode wn = okmMail.createWizard(null, "/okm:root/test", "test title", is, Mail.ORIGIN_EML);
System.out.print(wn);
IOUtils.closeQuietly(is);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
getExtractedText(String token, String uuid) String Returns a String with the extracted text from the text extractor process.
  • When a document is uploaded to OpenKM, it goes into the text extraction queue. There’s a crontab job that periodically processes this queue and extracts document contents.

Example:

package com.openkm;
import com.openkm.api.OKMMail;import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
String text = okmMail.getExtractedText(null, "46762f90-82c6-4886-8d21-ad3017dd78a7");
System.out.println(text);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Description:

Method Return values Description
forwardEmail(String token, String uuid, List users, List roles, List mails, String message) Mail Forwards an email to a list of users, roles, or external email addresses.

Example:

package com.openkm;
import java.util.ArrayList;
import java.util.List;
import com.openkm.api.OKMMail;
import com.openkm.bean.Mail;
import com.openkm.util.ContextWrapper;
public class Test {
public static void main(String[] args) {
try {
OKMMail okmMail = ContextWrapper.getContext().getBean(OKMMail.class);
String mailUuid = "f123a950-0329-4d62-8328-0ff500fd42db";
List<String> users = new ArrayList<>();
users.add("userTest");
List<String> roles = new ArrayList<>();
roles.add("ROLE_USER");
List<String> mails = new ArrayList<>();
mails.add("test@none.com");
Mail forwardedMail = okmMail.forwardEmail(null, mailUuid, users, roles, mails, "Any message");
System.out.println(forwardedMail);
} catch (Exception e) {
e.printStackTrace();
}
}
}