Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating folders in IMAP does not work

I am trying hard to create a folder in IMAP (I am using gmail)

Here is what i have attempted so far:

public boolean createFolder(String folderName) throws MessagingException
{
    store = session.getStore("imap");
    System.out.println("connecting store..");
    store.connect("imap.gmail.com", 993, "[email protected]", "password");
    System.out.println("connected !");

    //using store.getDefaultFolder(); shows error : "folder cannot contain messages"
    Folder defaultFolder = store.getFolder("Inbox");

    return createFolder(defaultFolder, folderName);
}

private boolean createFolder(Folder parent, String folderName)
{
    boolean isCreated = true;

    try
    {
        parent.open(Folder.READ_WRITE);
        System.out.println("adding listeners...");
        parent.addFolderListener(myMailFolderListener);
        System.out.println("creating a folder ....");
        Folder testFolder = parent.getFolder(folderName);

        isCreated = testFolder.create(Folder.HOLDS_MESSAGES);
        System.out.println("created: " + isCreated);
        parent.close(true);
    } catch (Exception e)
    {
        System.out.println("Error creating folder: " + e.getMessage());
        isCreated = false;
    }
    return isCreated;
} 

foldername passed: "test 123"

output: folder is created as Inbox/test123

what should i do in this line: Folder defaultFolder = store.getFolder("Inbox"); ??

I have also posted this same question here: http://www.coderanch.com/t/458722/Sockets-Internet-Protocols/java/creating-folders-IMAP-does-not

like image 355
Salvin Francis Avatar asked Aug 18 '09 08:08

Salvin Francis


People also ask

Can you create folders in IMAP?

To create a subfolder structure, right click on the iMap account name in the Thunderbird folder list. To create a real folder with a mail folder inside it, type in the real folder name followed by a forward slash (/) and then the name of the mail folder you would like created in that folder.

Why are my IMAP folders not syncing?

An unreliable and poor network connection or firewall settings can obstruct Outlook from synchronizing the mail items stored in the IMAP OST file to the mailbox server. Similarly, if the mailbox server is down, it can prevent Outlook from synchronizing the mail items.

Why can't I add a folder to an email?

You can't. A folder isn't a file so it can't be attached. What you can do is compressing the folder so it will create a zip-file. You can then attach the zip-file and send it.

How do IMAP folders work?

IMAP (Internet Message Access Protocol) Folders can be created on the server to better manage the messages. The folders then sync across all devices used to check your email. Sent messages are also saved in the sent folder, allowing you to view sent emails from any computer or device.


1 Answers

Here is the correct code :

public boolean createFolder(String folderName) throws MessagingException   
{   
    store = session.getStore("imap");   
    System.out.println("connecting store..");   
    store.connect("imap.gmail.com", 993, "[email protected]", "password");   
    System.out.println("connected !");   
    Folder defaultFolder = store.getDefaultFolder();   
    return createFolder(defaultFolder, folderName);   
}   

/*  
 * Note that in Gmail folder hierarchy is not maintained.  
 * */  
private boolean createFolder(Folder parent, String folderName)   
{   
    boolean isCreated = true;   

    try  
    {   
        Folder newFolder = parent.getFolder(folderName);   
        isCreated = newFolder.create(Folder.HOLDS_MESSAGES);   
        System.out.println("created: " + isCreated);   

    } catch (Exception e)   
    {   
        System.out.println("Error creating folder: " + e.getMessage());   
        e.printStackTrace();   
        isCreated = false;   
    }   
    return isCreated;   
}
like image 105
Salvin Francis Avatar answered Oct 08 '22 01:10

Salvin Francis