OKMMail
Basics
Section titled “Basics”In almost all methods you’ll see a parameter named “mailId”. The value of this parameter can be any valid mail UUID.
About the parameter named “fldId”, the value of this parameter can be any valid folder UUID or record node.
Also in all methods you’ll see a parameter named “token”. Because Cron tasks are 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 “null” value, which indicates the application must use the “user session”.
Methods
Section titled “Methods”getProperties
Section titled “getProperties”Description:
| Method | Return values | Description |
|---|---|---|
| getProperties(String token, String mailId) | 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(); } }}createAttachment
Section titled “createAttachment”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.poi.util.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); } }}deleteAttachment
Section titled “deleteAttachment”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(); } }}getAttachments
Section titled “getAttachments”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(); } }}delete
Section titled “delete”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(); } }}rename
Section titled “rename”Description:
| Method | Return values | Description |
|---|---|---|
| rename(String token, String mailId, String newName) | 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 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.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) | void | 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) | void | 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 its 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(); } }}extendedCopy
Section titled “extendedCopy”Description:
| Method | Return values | Description |
|---|---|---|
| extendedCopy(String token, String mailId, String dstId, ExtendedAttributes extAttr) | void | 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(); } }}extendedCopy
Section titled “extendedCopy”Description:
| Method | Return values | Description |
|---|---|---|
| extendedCopy(String token, String mailId, String dstId, ExtendedAttributes extAttr, String newName) | void | 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 parameter newName value is null, mail will preservate 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(); } }}getChildren
Section titled “getChildren”Description:
| Method | Return values | Description |
|---|---|---|
| getChildren(String token, String fldId) | List |
Returns a list of all mails whose parent is mailId. |
- The parameter mailId 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(); } }}isValid
Section titled “isValid”Description:
| Method | Return values | Description |
|---|---|---|
| isValid(String token, String mailId) | boolean | Returns a boolean that indicates if 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(); } }}getPath
Section titled “getPath”Description:
| Method | Return values | Description |
|---|---|---|
| getPath(String token, String uuid) | String | Converts a folder 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(); } }}sendMail
Section titled “sendMail”Description:
| Method | Return values | Description |
|---|---|---|
| sendMail(String token, List |
void | Sends a 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("gnu.java.sergio@gmail.com"); okmMail.sendMail(null, recipients, "Testing sending mail from OpenKM", body.toString()); } catch (Exception e) { e.printStackTrace(); } }}setTitle
Section titled “setTitle”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(); } }}sendMailWithAttachments
Section titled “sendMailWithAttachments”Description:
| Method | Return values | Description |
|---|---|---|
| sendMailWithAttachments(String token, List List |
Sends a mail message with an 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(); } }}importEml
Section titled “importEml”Description:
| Method | Return values | Description |
|---|---|---|
| importEml(String fldId, String title, InputStream is) | 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); } }}importMsg
Section titled “importMsg”Description:
| Method | Return values | Description |
|---|---|---|
| importMsg(String fldId, String title, InputStream is) | 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); } }}setNodeClass
Section titled “setNodeClass”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(); } }}sendMail
Section titled “sendMail”Description:
| Method | Return values | Description |
|---|---|---|
| sendMail(String token, String from, List |
void | Sends a mail with a specified sender address. |
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"); String from = "sender@example.com"; okmMail.sendMail(null, from, recipients, "Testing sending mail from OpenKM", body.toString()); } catch (Exception e) { e.printStackTrace(); } }}sendMailWithAttachments
Section titled “sendMailWithAttachments”Description:
| Method | Return values | Description |
|---|---|---|
| sendMailWithAttachments(String token, String from, List List |
Sends a mail message with an attachment and a specified sender address. |
- 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"); String from = "sender@example.com";
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(); } }}MethodReturn valuesDescription
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(); } }}setDescription
Section titled “setDescription”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, "b0747c0cb-5e4a-4088-92a9-ab97b401af75"); System.out.println(mail); } catch (Exception e) { e.printStackTrace(); } }}getContent
Section titled “getContent”Description:
| Method | Return values | Description |
|---|---|---|
| getContent(String token, String mailId) | InputStream | Retrieve 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(); } }}createWizard
Section titled “createWizard”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(); } }}getExtractedText
Section titled “getExtractedText”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 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(); } }}forwardEmail
Section titled “forwardEmail”Description:
| Method | Return values | Description |
|---|---|---|
| forwardEmail(String token, String uuid, List |
void | Forward 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.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");
okmMail.forwardEmail(null, mailUuid, users, roles, mails, "Any message"); } catch (Exception e) { e.printStackTrace(); } }}