Mail samples
Basics
Section titled “Basics”On almost methods you’ll see parameter named “mailId”. The value of this parameter can be a valid mail UUID or path.
Methods
Section titled “Methods”createMail
Section titled “createMail”Description:
| Method | Return values | Description |
|---|---|---|
| createMail(Mail mail) | Creates a new mail and returns as a result an object Mail. |
The variable path into the parameter mail, must be initializated. It indicates the folder path into OpenKM.
Other mandatory variables:
- size ( mail size in bytes ).
- from ( mail from account ).
- reply, to, cc, bcc ( mail accounts are optional ).
- sendDate ( date when mail was sent ).
- receivedDate ( date when was received ).
- subject ( mail subject ).
- content ( the mail content ).
- mimeType ( HTML or text mime type ).
- headers ( the mail headers are optional).
- raw ( the mail raw are optional).
- origin ( msg, eml, api, pop3, imap origin )
- title ( mail title )
Example:
package com.openkm;
import java.util.Arrays;import java.util.Calendar;
import org.owasp.encoder.Encode;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Mail;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { Mail mail = new Mail();
// Mail path = msgId + escaped(subject) String msgId = "2937b81d-0b10-4dd0-a426-9acbd80be1c9"; String subject = "some subject"; String mailPath = "/okm:root/"+ msgId + "-" + escape(subject); mail.setPath(mailPath);
// Other format for mail "some name <no_reply@openkm.com>" mail.setFrom("<no_reply@openkm.com>"); mail.setTo((String[])Arrays.asList("anonymous@gmail.com").toArray());
// You should set real dates mail.setSentDate(Calendar.getInstance()); mail.setReceivedDate(Calendar.getInstance()); mail.setContent("some content"); mail.setMimeType(Mail.MIME_TEXT); mail.setSubject(subject); mail.setOrigin(Mail.ORIGIN_API);
// Get only as an approximation of real size for these sample mail.setSize(mail.toString().getBytes("UTF-8").length); ws.createMail(mail); } catch (Exception e) { e.printStackTrace(); } }
private static String escape(String name) { String ret = cleanup(name);
// Fix XSS issues ret = Encode.forHtml(ret);
return ret; }
private static String cleanup(String name) { String ret = name.replace("/", ""); ret = ret.replace("*", ""); ret = ret.replaceAll("\\s+", " ").trim(); return ret; }}getMailProperties
Section titled “getMailProperties”Description:
| Method | Return values | Description |
|---|---|---|
| getMailProperties(String mailId) | Returns the mail properties. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { System.out.println(ws.getMailProperties("064ff51a-b815-4f48-a096-b4946876784f")); } catch (Exception e) { e.printStackTrace(); } }}deleteMail
Section titled “deleteMail”Description:
| Method | Return values | Description |
|---|---|---|
| deleteMail(String mailId) | void | Deletes a mail. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.deleteMail("064ff51a-b815-4f48-a096-b4946876784f"); } catch (Exception e) { e.printStackTrace(); } }}purgeMail
Section titled “purgeMail”Description:
| Method | Return values | Description |
|---|---|---|
| purgeMail(String mailId) | void | Mail is definitely removed from repository. |
Usually you will purge mails into /okm:trash/userId - the personal trash user locations - but it is possible to directly purge any mail from the whole repository.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.purgeMail("064ff51a-b815-4f48-a096-b4946876784f"); } catch (Exception e) { e.printStackTrace(); } }}renameMail
Section titled “renameMail”Description:
| Method | Return values | Description |
|---|---|---|
| renameMail(String mailId, String newName) | void | Rename a mail. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.renameMail("064ff51a-b815-4f48-a096-b4946876784f","new name"); } catch (Exception e) { e.printStackTrace(); } }}moveMail
Section titled “moveMail”Description:
| Method | Return values | Description |
|---|---|---|
| moveMail(String mailId, String dstId) | void | Move mail into a folder or record. |
The values of the dstId parameter should be a folder or record UUID or path.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.moveMail("064ff51a-b815-4f48-a096-b4946876784f","/okm:root/tmp"); } catch (Exception e) { e.printStackTrace(); } }}copyMail
Section titled “copyMail”Description:
| Method | Return values | Description |
|---|---|---|
| public void copyMail(String mailId, String dstId, String newName) | void | Copies mail into a folder or record. |
The values of the dstId parameter should be a folder or record UUID or path.
When parameter newName value is null, mail will preservate the same name.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.copyMail("064ff51a-b815-4f48-a096-b4946876784f","/okm:root/temp","new_name"); } catch (Exception e) { e.printStackTrace(); } }}extendedMailCopy
Section titled “extendedMailCopy”Description:
| Method | Return values | Description |
|---|---|---|
| extendedMailCopy(String mailId, String dstId, boolean categories, boolean keywords, boolean propertyGroups, boolean notes, boolean wiki, String newName) |
void | Copy mail width with associated data into a folder or record. |
The values of the dstId parameter should be a folder or record UUID or path.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.extendedMailCopy("064ff51a-b815-4f48-a096-b4946876784f", "/okm:root/tmp", true, true, true, true, true, null); } catch (Exception e) { e.printStackTrace(); } }}getMailChildren
Section titled “getMailChildren”Description:
| Method | Return values | Description |
|---|---|---|
| getMailChildren(String fldId) | List |
Returns a list of all mails which their parent is fldId. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Mail;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (Mail mail : ws.getMailChildren("/okm:root")) { System.out.println(mail); } } catch (Exception e) { e.printStackTrace(); } }}isValidMail
Section titled “isValidMail”Description:
| Method | Return values | Description |
|---|---|---|
| isValidMail(String mailId) | Boolean | Returns a boolean that indicates if the node is a mail or not. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { // Return false ws.isValidMail("/okm:root/logo.png");
// Return true ws.isValidMail("064ff51a-b815-4f48-a096-b4946876784f"); } catch (Exception e) { e.printStackTrace(); } }}getMailPath
Section titled “getMailPath”Description:
| Method | Return values | Description |
|---|---|---|
| getMailPath(String uuid) | String | Converts mail UUID to mail path. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { System.out.println(ws.getMailPath("064ff51a-b815-4f48-a096-b4946876784f")); } catch (Exception e) { e.printStackTrace(); } }}createAttachment
Section titled “createAttachment”Description:
| Method | Return values | Description |
|---|---|---|
| createAttachment(String mailId, String docName, InputStream is) | Document | Adds an attachement to the mail. |
Example:
package com.openkm;
import java.io.FileInputStream;import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { InputStream is = new FileInputStream("/home/files/logo.png"); ws.createAttachment("064ff51a-b815-4f48-a096-b4946876784f", "log.png", is); IOUtils.closeQuietly(is); } catch (Exception e) { e.printStackTrace(); } }}deleteAttachment
Section titled “deleteAttachment”Description:
| Method | Return values | Description |
|---|---|---|
| deleteAttachment(String mailId, String docId) | void | Delete a mail attachment. |
The value of the parameter docId parameter can be a valid document UUID or path.
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { ws.deleteAttachment("064ff51a-b815-4f48-a096-b4946876784f", "f123a950-0329-4d62-8328-0ff500fd42db"); } catch (Exception e) { e.printStackTrace(); } }}getAttachments
Section titled “getAttachments”Description:
| Method | Return values | Description |
|---|---|---|
| getAttachments(String mailId) | List |
Retrieves a list of all the attachment documents of the mails. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Document;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { for (Document doc : ws.getAttachments("064ff51a-b815-4f48-a096-b4946876784f")) { System.out.println(doc); } } catch (Exception e) { e.printStackTrace(); } }}sendMailWithAttachments
Section titled “sendMailWithAttachments”Description:
| Method | Return values | Description |
|---|---|---|
| sendMailWithAttachments(List |
void | Send mail message with attachement. |
The values of the dstId parameter should be a folder or record UUID or path. The dstId parameter indicate where the mail will be stored in the repository after is sent.
Other parameters:
- to are a list of mail accounts destination.
- subject is the mail subject.
- cc, bcc are a list of mail accounts destination ( optional )
- docsId are a list of valid document UUID already into OpenKM that will be send as attachment into mail ( optional ).
Example:
package com.openkm;
import java.util.ArrayList;import java.util.Arrays;import java.util.List;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { List<String> to = Arrays.asList("destination@mail.com"); List<String> cc = new ArrayList<>(); List<String> bcc = new ArrayList<>(); List<String> docsId = Arrays.asList("f123a950-0329-4d62-8328-0ff500fd42db","/okm:root/logo.png"); ws.sendMailWithAttachments(to, cc, bcc, "some subject", "some body", docsId, "/okm:root/tmp"); } catch (Exception e) { e.printStackTrace(); } }}importEml
Section titled “importEml”Description:
| Method | Return values | Description |
|---|---|---|
| importEml(String dstId, String title, InputStream is) | Import a mail in EML format. |
The values of the dstId parameter should be a folder or record UUID or path. The dstId parameter indicate where the mail will be stored in the repository after is imported.
Example:
package com.openkm;
import java.io.FileInputStream;import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Mail;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { InputStream is = new FileInputStream("/home/files/test.eml"); Mail mail = ws.importEml("d88cff0d-903a-4c5a-82ea-8e6dbd100d65", "some title", is); System.out.println(mail); IOUtils.closeQuietly(is); } catch (Exception e) { e.printStackTrace(); } }}importMsg
Section titled “importMsg”Description:
| Method | Return values | Description |
|---|---|---|
| importMsg(String dstId, String title, InputStream is) | Import a mail in MSG format. |
The values of the dstId parameter should be a folder or record UUID or path. The dstId parameter indicate where the mail will be stored in the repository after is sent.
Example:
package com.openkm;
import java.io.FileInputStream;import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;import com.openkm.sdk4j.bean.Mail;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password);
try { InputStream is = new FileInputStream("/home/files/test.msg"); Mail mail = ws.importMsg("d88cff0d-903a-4c5a-82ea-8e6dbd100d65", "some title", is); System.out.println(mail); IOUtils.closeQuietly(is); } catch (Exception e) { e.printStackTrace(); } }}setMailTitle
Section titled “setMailTitle”Description:
| Method | Return values | Description |
|---|---|---|
| setMailTitle(String mailId, String title) | void | Sets the mail title. |
Example:
package com.openkm;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { ws.setMailTitle("68ed7a93-005c-4ec6-ac01-bb151573c775", "new title"); } catch (Exception e) { e.printStackTrace(); } }}sendMail
Section titled “sendMail”Description:
| Method | Return values | Description |
|---|---|---|
| sendMail(List |
void | Sends a mail. |
Example:
package com.openkm;
import java.util.ArrayList;import java.util.List;
import com.openkm.sdk4j.OKMWebservices;import com.openkm.sdk4j.OKMWebservicesFactory;
public class Test { public static void main(String[] args) { String host = "http://localhost:8080/OpenKM"; String username = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host, username, password); try { StringBuffer body = new StringBuffer(); body.append("Test message body."); List<String> recipients = new ArrayList<>(); recipients.add("some@mail.com"); ws.sendMail(recipients, "Testing sending mail from OpenKM", body.toString()); } catch (Exception e) { e.printStackTrace(); } }}