1

How to read mail inbox using IMAP protocol and JavaMail and then use local disk to store mails. There is no documentation of mstor. I try this way but it seems that MStorStore just read local mbox instead of creating and updating it according to the external server passed as params in connect() function. I get error: Folder [Inbox] does not exist.

Session lSession = Session.getDefaultInstance(props);
MStorStore lStore = new MStorStore(lSession , new URLName("mstor:c:/some_path/" + _mailModel.account.login));
lStore.connect(_mailModel.account.imap, _mailModel.account.login, _mailModel.account.password);
Folder lInbox = lStore.getDefaultFolder().getFolder("Inbox");

The questioin is how to create MBox from javax.mail.Store that i could read and update using Mstor.

Bartek Szczypien
  • 333
  • 4
  • 17
  • After having a quick look [MStorFolder](https://github.com/benfortuna/mstor/blob/master/src/main/java/net/fortuna/mstor/MStorFolder.java) might be what you are looking for. – SubOptimal Mar 04 '16 at 08:55
  • No, MStorFolder extends java.mail.Folder and from what i see it is only to read from folder that already exist – Bartek Szczypien Mar 04 '16 at 09:34
  • [MStorFolder.create](https://github.com/benfortuna/mstor/blob/master/src/main/java/net/fortuna/mstor/MStorFolder.java#L206) sounds like creating a folder. If it exists an exceptioin `new MessagingException("Folder already exists")` would be thrown. Sounds not to bad for me. – SubOptimal Mar 04 '16 at 09:39
  • yes but the problem is MStorStore which is passed as argument in constructor to MStorFolder use either mstor protocol (local storage) or imaps and i cant download mbox from server and keep it sync – Bartek Szczypien Mar 04 '16 at 09:51

1 Answers1

0

I don't know if I am answering the right question (or answering a question at all), but, here is a method I wrote in a Scala program that takes an array of javamail Messages (acquired via imap) and writes them to a new mbox file in a directory named "mbox" in the root of my project using MStorStore. The new file is named whatever is passed in the "mboxName" parameter.

def writeToMbox(messages: Array[Message], mboxName: String) {    
    val mProps = System.getProperties
    mProps.setProperty("mstor.mbox.metadataStrategy", "none")
    val mSession = Session.getDefaultInstance(mProps)
    val mStore = new MStorStore(mSession, new URLName("mstor:mbox"))
    mStore.connect
    val mFolder = mStore.getDefaultFolder
    val localMbox = (new File("mbox", mboxName)).createNewFile
    val mbox = mFolder.getFolder(mboxName)
    mbox.open(Folder.READ_WRITE)
    mbox.appendMessages(messages)
    mbox.close(false)
    mStore.close
  }
awfulHack
  • 857
  • 1
  • 10
  • 25