The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications.

The mail read package provides methods to connect to IMAP or POP3 accounts, retrieve mails and extract their content.

1 Mail Store

Mail store defines a connection to a mail server using one of the supported protocols: imap, imaps, pop, pops

1.1 JSMailStore Class

Return Options Description
JSMailStore Ax.mail.Store(String protocol, options) The constructor receives a parameter providing mail communication protocol. Protocols provided are: pop, pops, imap and imaps.
String getURLName() Get connection URL
JSMailStore connect(String host, String user, String password) Connects to host server using protocol defined in constructor.
JSMailStore connect(String host, int Port, String user, String password) Connects to host server using protocol defined in constructor.
List<JSMailFolder> listFolders() Gets folders in Server.
JSMailFolder getFolder(String folder) gets e-mail folder class pointing to folder passed as parameter.
JSMailFolder getDefaultFolder .
List<JSMailFolder> getPersonalNamespaces() .
List<JSMailFolder> getSharedNamespaces() .

1.2 JSMailFolder Class

Return Options Description
JSMailMessage Ax.mail.MailMessage(String ContentRFC) The constructor receives a parameter providing mail in RFC String format generated by Ax.mail.MailerMessage().getMessageContent(null).Returns an JSMailMessage.
List<JSMailMessage> expunge() Expunge (permanently remove) messages marked DELETED. Returns an array containing the expunged message objects.
String getName() Get Folder name
String getFullName() Returns the full name of this Folder. If the folder resides under the root hierarchy of this Store, the returned name is relative to the root. Otherwise an absolute name, starting with the hierarchy delimiter, is returned.
int getNewMessageCount Get the number of new messages in this Folder.
int getUnreadMessageCount Get the number of unreaded messages in folder.
JSMailFolder open(boolean write) Open this Folder. open the Folder READ_ONLY or READ_WRITE.
JSMailMessage[] getMessages() Read all messages in folder and mark them as seen. Returns an empty array if the folder is empty.
JSMailMessage[] getMessages(boolean markSeen) Get all Message objects from this Folder. Returns an empty array if the folder is empty.
JSMailMessage[] getMessages(boolean markSeen) Get all Message objects from this Folder. Returns an empty array if the folder is empty.
List<JSMailFolder> listFolders() Gets folders depending of this folder.
List<JSMailFolder> listFolders(String pattern) Gets folders depending of this folder which comply with pattern.
void copyMessages(JSMailMessage[] msgs, JSMailFolder folder) Copy messages in array to other folder.

1.3 JSMailMessage Class

Return Options Description
JDBCBlob toBlob() Return the message as binary content (the full mail message).
boolean isSet(Flag flag) Check whether the flag specified in the flag argument is set in this message. Flags supported: Ax.mail.Message.SEEN, Ax.mail.Message.DRAFT, Ax.mail.Message.ANSWERED, Ax.mail.Message.DELETED, Ax.mail.Message.DRAFT, Ax.mail.Message.FLAGGED, Ax.mail.Message.RECENT
void setFlag(Flag flag, boolean set) Set the specified flag on this message to the specified value.
String getMessageID() Get ID of Message.
int getMessageNumber() .
int getSize() Get message size.
String getFrom() Get from part of message.
String getTo() Get to part of message.
String getCC() Get CC part of message.
String getBCC() Get BCC part of message.
String getReplyTo() Get Reply-To part of message.
String getAllRecipients() Get all recipients of message.
String getSubject() Get subject of message.
Date getSentDate() Get sent date of message.
Date getReceivedDate() Get received date of message.
String getFileName() Get the filename associated with this part, if possible. Useful if this part represents an "attachment" that was loaded from a file.
String getContentType() Get the content type of this part.
String getDisposition() .
String getDescription() .
Object getContent() Return the content as a Java object. The type of the returned object is of course dependent on the content itself. For example, the object returned for "text/plain" content is usually a String object. The object returned for a "multipart" content is always a Multipart subclass.
String getMessageContent() Output the message as an RFC 822 format stream.
String getMessageContent(String[] ignoreList) Output the message as an RFC 822 format stream, without specified headers.
String getMessageContentText() Return the text content of the message. Only retrieves the text/plain part of message.
String getMessageContentHtml() Return the text content of the message. Only retrieves the text/html part of message.
ArrayList<JSBlob> getMessageParts() Get the files attached to message as an array of blobs.

2 Gmail example

Copy
<script>
console.log("==============: Connecting to mail server")
var store = new Ax.mail.Store("imaps", properties => {
	properties.put("debug", "false");
	});

store.connect("imap.gmail.com", 993, "*****", "*****");


console.log("==============: Store")
for (var folder of store.listFolders()) {
    console.log("        folder:" + folder.getName() + " / " + folder.getFullName());
}
console.log("    personalNS:" + store.getPersonalNamespaces());
console.log("      sharedNS:" + store.getSharedNamespaces());
console.log("           URL:" + store.getURLName());


// Open Inbox (READ+WRITE)
console.log("==============: Open inbox")
var inbox = store.getFolder("Inbox").open(true);
console.log("          name:" + inbox.getName());
console.log("      fullname:" + inbox.getFullName());
console.log("unreadMessages:" + inbox.getUnreadMessageCount());

// Get messages marking them as SEEN
console.log("==============: Reading message")
var messages = inbox.getMessages(true);
for (var message of messages) {
    
    message.setFlag(Ax.mail.Message.FLAGGED, true);
    
    console.log("==============: ")
    console.log("        toBlob: " + message.toBlob())
    console.log("     MessageID: " + message.getMessageID())
    console.log(" MessageNumber: " + message.getMessageNumber())
    console.log("          From: " + message.getFrom())
    console.log("            CC: " + message.getCC())
    console.log("           BCC: " + message.getBCC())
    console.log("       Subject: " + message.getSubject())
    console.log(" AllRecipients: " + message.getAllRecipients())
    console.log("  Content-Type: " + message.getContentType())
    console.log("          Size: " + message.getSize())
    console.log("        isSeen: " + message.isSet(Ax.mail.Message.SEEN))
    console.log("       isDraft: " + message.isSet(Ax.mail.Message.DRAFT))
    console.log("    isAnswered: " + message.isSet(Ax.mail.Message.ANSWERED))
    console.log("     isDeleted: " + message.isSet(Ax.mail.Message.DELETED))
    console.log("       isDraft: " + message.isSet(Ax.mail.Message.DRAFT))
    console.log("     isFlagged: " + message.isSet(Ax.mail.Message.FLAGGED))
    console.log("      isRecent: " + message.isSet(Ax.mail.Message.RECENT))
    console.log("       Content: " + message.getContent())
    console.log("   MessageText: " + message.getMessageContentText())
    console.log("   MessageHtml: " + message.getMessageContentHtml())	
    console.log("        ------ parts -----");
    // Get message parts
    for (var blob of message.getMessageParts()) {
        if (blob.isInline()) {
            console.log("        INLINE: " + blob);
        } else if (blob.isAttachment()) {
            console.log("    ATTACHMENT: " + blob);
        } else {
            console.log("          BODY: " + blob);
            console.log("          TEXT: " + blob.getTextContent());
        }
    }
}

// We cannot delete mails in Gmail programmatically.  
// All we can do is to move the mails from Inbox to Trash folder which 
// then would get deleted automatically after 30 days.
//
// Others GMail folders :
// * [Gmail]/Inbox      Inbox message.
// * [Gmail]/All Mail   This folder contains all of your Gmail messages.
// * [Gmail]/Drafts     Your drafts.
// * [Gmail]/Sent Mail  Messages you sent to other people.
// * [Gmail]/Spam       Messages marked as spam.
// * [Gmail]/Starred    Starred messages.
// * [Gmail]/Trash      Messages deleted from Gmail.

// Open Trash (READ+WRITE)
if (false) {
    var trash = store.getFolder("[Gmail]/Trash").open(true);
    inbox.copyMessages(messages, trash);
}
</script>

3 iMAP example

This imap example show how you can iterate to get folders beneeth folders in main level.

Copy
<script>
    function show_messages(folder) {
        folder.open(false); // Open read-only
        
        var messages = folder.getMessages(false); // do not mark as seen
        for (var message of messages) {
            console.log("==============: ")
            console.log("     MessageID: " + message.getMessageID())
            console.log(" MessageNumber: " + message.getMessageNumber())
            console.log("          From: " + message.getFrom())
            console.log("            CC: " + message.getCC())
            console.log("           BCC: " + message.getBCC())
            console.log("       Subject: " + message.getSubject())
            console.log(" AllRecipients: " + message.getAllRecipients())
            console.log("  Content-Type: " + message.getContentType())
            console.log("          Size: " + message.getSize())
            console.log("        isSeen: " + message.isSet(Ax.mail.Message.SEEN))
            console.log("       isDraft: " + message.isSet(Ax.mail.Message.DRAFT))
            console.log("    isAnswered: " + message.isSet(Ax.mail.Message.ANSWERED))
            console.log("     isDeleted: " + message.isSet(Ax.mail.Message.DELETED))
            console.log("       isDraft: " + message.isSet(Ax.mail.Message.DRAFT))
            console.log("     isFlagged: " + message.isSet(Ax.mail.Message.FLAGGED))
            console.log("      isRecent: " + message.isSet(Ax.mail.Message.RECENT))
        }
        folder.close();
    }

    var store = new Ax.mail.Store("imaps")
       .connect("mail-es.securemail.pro", "*****", "*****");

    // Get messages from folders level 2 deep
    for (var folder of store.listFolders()) {
        console.log("        folder:" + folder.getName() + " / " + folder.getFullName());
        console.log("--------------------------------------------");
        show_messages(folder);
        
        // Get subfolders (level 2 deep) of main folders
    	for (var folder2 of folder.list()) {
            console.log("        folder2:" + folder2.getName() + " / " + folder2.getFullName());
            console.log("--------------------------------------------");
            show_messages(folder2);
        }
    }
</script>