ImapClientMk(config)
Creates an IMAP client and connects to the server: new docly.ImapClientMk(config). The client keeps the connection open, so several calls can share it. Call disconnect() when you are done.
Methods:
getCount([folder])— the number of messages in the folder (default the inbox).getMessage(indexOrMessageId, [headersOnly], [folder])— one message, by its 0-based index (a whole number) or its Message-ID (a string). Throws "Message not found." for an unknown Message-ID.delete(message)— deletes a message object returned by getMessage from the inbox. Returns true, or false when the message is not found.disconnect()— closes the connection. Returns true, or false when it was already closed.
Parameters
configobject | stringrequiredConnection settings: hostname, port, useSsl, username, password and method. useSsl: true connects with TLS from the start; false (default) uses STARTTLS when the server offers it. method is Login (default), CRAMMD5, SaslOAuth or OAuth2 — for OAuth, password is the access token. Default port 143. Or pass the path to a document under #/ that holds these values, such as "#/My Imap": a path reads the values of that document, the same as docly.getJson("#/My Imap"). Throws "Mail configuration document not found" if there is no such document.
Returns
The client object with the methods above. Throws if the connection or the login fails. A message object has MessageId, Date, From, To, Cc, Bcc, Subject, HtmlBody, TextBody, Body (the HTML body, or the text body when there is none), Headers (an object of header name and value) and Attachments — a list of { FileName, ContentType, ContentId, Base64, Headers }.
Example
let imap = new docly.ImapClientMk({
"hostname" : "imap.example.com",
"port" : 993,
"useSsl" : true,
"username" : "login",
"password" : "pass"
});
try {
if (imap.getCount() == 0) return "No emails in inbox";
let msg = imap.getMessage(0);
imap.delete(msg);
return { subject: msg.Subject, body: msg.Body };
} finally {
imap.disconnect();
}