2009-08-18 17 views
7

Estoy tratando duro para crear una carpeta en IMAP (estoy usando Gmail)crear carpetas de IMAP no funciona

Aquí es lo que he intentado hasta ahora:

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 pasado: " prueba de 123"

de salida: carpeta se crea como Bandeja de entrada/test123

¿qué debo hacer en esta línea: carpeta defaultFolder = store.getFolder ("Entrada"); ??

También he publicado esta misma pregunta aquí: http://www.coderanch.com/t/458722/Sockets-Internet-Protocols/java/creating-folders-IMAP-does-not

Respuesta

13

Aquí es el código correcto:

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; 
}