B4A Library Apache Commons Net - IMAP

Currently in progress of completing an IMAP wrapper for Apache Commons net 3.3, it is not complete as I wrote it to implement functions I needed at the time but I will add features as I go :)

Not all functions and documentation implemented or tested fully yet.


1. Download commons-net-3.3.jar from Apache Commons
2. Copy the downloaded jar file from the zip to B4A or B4J libraries folder
3. Download attached library, unzip it and copy jar and xml to the libraries folder

Should catch most errors, I'll be adding more functions and tidying code as I go....:)

Hope it's of use to someone!

Latest version : v0.37

Implemented so far:
  • Connections - connect, logout, disconnect, connection timeout, socket timeout,
  • append, check, copy, create, delete, examine, expunge, fetch, list, search, select, size, status, store, subscribe, uid, unsubscribe

B4X:
    Dim ic As ApacheCommonsNet

    ic.connectionDefaultTimeout = 10000
    ic.IMAPsClientConnect("imap.gmail.com", 993, True, "", "", "[email protected]", "xxxx")
    ic.socketOpenTimeout = 6000
 
    ' LIST / LSUB
    ' Boolean for subscribed only
    Dim l3 As List : l3.Initialize
    l3 = ic.list("[Gmail]", "*", False) ' Get children
    l3 = ic.list("", "%", True) ' Parent of subscribed
    l3 = ic.list("", "[Gmail]/%", False)

    ' SELECT
    ic.select("inbox")

    ' SEARCH
    ' Search returns formatted string of message UIDs
    ' Use single quotes around strings inside string

    Dim search As String
    search = ic.search("FROM '[email protected]' SUBJECT 'You've won the lottery!'")
 
    ' FETCH
    ' Search sequence can be single (eg. 1,4,6), range (eg. 20:30)  or mix of both
    ' * will list all messages (eg. 1:*)
    ' Last two params are start and end byte size. Use 0 to download all bytes.
 
    Dim l2 As List : l2.Initialize
    l2 = ic.fetch(search, "BODY[HEADER]", 0, 0)

    Dim l3 As List : l3.Initialize
    l3 = ic.fetch("1,5,10:15,28,33", "BODY[]", 0, 40)
 
    ' STORE
    ic.store("60:64", "+FLAGS", "\Flagged")

    ' CREATE
    ic.create("Henry") 'create parent - Gmail doesn't require hierarchy separator

    ' EXAMINE
    ic.select("Henry") 'select mailbox
    Log(ic.examine("Henry"))

    ' CHECK
    ic.check()
 
    ' COPY
    ic.copy("1:5", "Henry")

    ' DELETE
    ic.delete("Rubbish")

    ' SUBSCRIBE / UNSUBSCRIBE 
    ic.subscribe("Rubbish")
    ic.unsubscribe("Rubbish")


    ' EXPUNGE
    ic.expunge()

    ic.logout
    ic.disconnect

Useful IMAP commands HERE

v0.15 - Basic functionality - connections, fetch, search, list
v0.16 - Store, changed method names, error catching
v0.20 - Create, delete, examine mailboxes
v0.30 - Various commands added
v0.35 - Error fixes
v0.36 - Changed the way search works - now returns formatted string
v0.37 - Tidied code, fixed errors with returned values
 

Attachments

  • IMAPSSL_0.36.zip
    7.5 KB · Views: 240
  • IMAPSSL_0.37.zip
    7.5 KB · Views: 298
Last edited:

inakigarm

Well-Known Member
Licensed User
Longtime User
Great job !! Take in account that you must authorize at https://www.google.com/settings/security/lesssecureapps access to gmail mailbox from "unsecured apps"

If access from less secured apps are not authorized, it's impossible to connect to gmail mailbox (see error log)

B4X:
Program started.
Setting up connection to imap.gmail.com:993...
Def Connection Timeout: 10000
Trying to connect...
Connected to server...
* OK Gimap ready for requests from 79.150.69.77 b77mb194614467wmb
Logging in...
Couldn't connect to server...
* NO [WEBALERT https://accounts.google.com/ContinueSignIn?sarp= ----------------------------------------------------
Web login required.
AAAA NO [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)
Failed to connect to server...
Connection needs to be made before setting...
Refname:
Escaped out: ""
Subscribed Folders:
Finally
main._appstart (java line: 63)
java.lang.NullPointerException
    at org.apache.commons.net.imap.IMAP.sendCommandWithID(IMAP.java:229)
    at org.apache.commons.net.imap.IMAP.sendCommand(IMAP.java:247)
    at org.apache.commons.net.imap.IMAP.sendCommand(IMAP.java:272)
    at org.apache.commons.net.imap.IMAP.doCommand(IMAP.java:285)
    at org.apache.commons.net.imap.IMAPClient.lsub(IMAPClient.java:203)
    at commons_net_3_3.IMAP_Client.listMailbox(IMAP_Client.java:217)
    at b4j.example.main._appstart(main.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

If access from less secured apps are not authorize, it's impossible to connect to gmail mailbox
 

TheJinJ

Active Member
Licensed User
Longtime User
Great job !! Take in account that you must authorize at https://www.google.com/settings/security/lesssecureapps access to gmail mailbox from "unsecured apps"

If access from less secured apps are not authorized, it's impossible to connect to gmail mailbox (see error log)........

If access from less secured apps are not authorize, it's impossible to connect to gmail mailbox

Yep, pain really. Only tested with GMail so far, not sure if other providers use a similar setup.

How to enable 'less secure apps' in GMail
 
Last edited:

TheJinJ

Active Member
Licensed User
Longtime User
Updated to include most usable commands and fixed various errors. Gmail has a lot of peculiarities with regards to labels etc. Would be interested to have some feedback by people putting it to real use :)
 

peacemaker

Expert
Licensed User
Longtime User
Wow! Lib is created! THanks to the author !

Do you know - is it possible to use it for multi-user access to the single IMAP account ?
To make a small client app that can connect to the shared IMAP account and get a single "server" shared email at the same time ?

Or if one client is connected - another cannot ?
 

RobM

Member
Licensed User
Longtime User
I searched high an low for such a library, thank you.

I have been using your library to connect to an office365 account at work. Has all the functionality I require and have had real no issues.
Apart from one, sometimes it takes a while to authenticate and can freeze the UI. Once logged on it works flawlessly.
I have got around this by using the threading library just to log on, over kill for such a small task.

Is there any chance of an update to include Async connections? Or am I missing something.
 
Top