Good afternoon,

I am in the process of learning java and I am a little bit stock on a couple of issues. I am working with javamail version-1.4.2 and JavaBeans Activation Framework 1.0.2 release.
The purpose of my code is to give me the ability to move e-mails from one IMAP e-mail account to a different IMAP e-mail account.
I am able to copy all the messages from the source account to the destination, however when I arrive at the end of the copying
I receive this error message:"javax.mail.FolderClosedException" the folder shouldn't be closed because I close the folder at the very end of the code.
I would like to say that all mails are indeed copied over but I do get that error message.
Could someone tell me my mistake please?

Thank you

John,

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import java.util.Scanner;

public class IMAPProject 
{
	 //senders "S" , Dest is for the destination
	static String emailS;
	static String emailDest;//Destination variable
	static String passwordS;
	static String passwordDest;//Destination variable
	static String serverNameS;
	static String serverNameDest;//Destination variable
	static Session session = null;
	static Session sessionDest = null;//Destination variable
	static Store store = null;
	static Store storeDest = null;//Destination variable
	static Folder folder = null;
	static Folder folderDest = null;//Destination variable
	static Message message = null;
	static Message messageDest = null;//Destination variable
	static Message[] messages = null;
	static Message[] messagesDest = null;//Destination variable
	static String sender = null;
	static String senderDest = null;//Destination variable
	static String email = null;
	static String EmailDest = null;//Destination variable
	static String subject = null;	
	static String subjectDest = null;//Destination variable
	static int n ;
	static int nDest ;//Destination variable
	static Scanner input = new Scanner(System.in);
	
	//Source e-mail account
	public static void main (String args[])
	{

	System.out.print("Source e-mail account:");
	emailS=input.next();
	
	System.out.print("Source password:");
	passwordS=input.next();
	
	System.out.print("Source server name");
	serverNameS=input.next();
	
	//destination
	System.out.print("Destination e-mail account:");
	emailDest=input.next();
	
	System.out.print("Destination password:");
	passwordDest=input.next();
	
	System.out.print("Destination server name");
	serverNameDest=input.next();
	
	try {
	//establishing source connection
	session = Session.getDefaultInstance(System.getProperties(), null);
	store = session.getStore("imaps");
	store.connect(serverNameS,emailS,passwordS);
	
	//establishing destination connection
	sessionDest = Session.getDefaultInstance(System.getProperties(), null);
	storeDest = sessionDest.getStore("imaps");
	storeDest.connect(serverNameDest,emailDest,passwordDest);
	
	//establishing the source default folder
	folder = store.getDefaultFolder();
	folder = folder.getFolder("inbox");
	
	//establishing the destination default folder
	folderDest = storeDest.getDefaultFolder();
	folderDest = folderDest.getFolder("inbox");
	
	//reading the source e-mails from the inbox
	folder.open(Folder.READ_WRITE);
	
	//reading the destination e-mails from the inbox
	folderDest.open(Folder.READ_WRITE);
	
	//getting the source messages
	messages = folder.getMessages();
	
	//getting the destination messages
	messagesDest = folderDest.getMessages();
	
	for (int messageNumber = 0; messageNumber < messages.length; messageNumber++)
	{//source
		message = messages[messageNumber];
		
		
			sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
			email = ((InternetAddress) message.getFrom()[0]).getAddress();
			Date date = message.getReceivedDate();
			if (sender == null)
			{ 
				sender = ((InternetAddress) message.getFrom()[0]).getAddress();
			}
			System.out.print("Sender - " + sender);
			System.out.print(" email - " +email + date +"\n");
			
			}
	        //source
			System.out.print("Enter source e-mail message number to delete:");
			n=input.nextInt();
			deleteSourceEmail(n);
	
			
			//end for source 
			
			
			//begin destination
			for (int messageNumberDest = 0; messageNumberDest < messagesDest.length; messageNumberDest++)
			{//source
			messageDest = messagesDest[messageNumberDest];
			senderDest = ((InternetAddress) messageDest.getFrom()[0]).getPersonal();
			System.out.println(senderDest);
			emailDest = ((InternetAddress) messageDest.getFrom()[0]).getAddress();
			Date dateDest = messageDest.getReceivedDate();
			if (senderDest == null)
			{ 
				senderDest = ((InternetAddress) messageDest.getFrom()[0]).getAddress();
			}
			System.out.print("Sender - " + senderDest);
			System.out.print(" email - " +emailDest + dateDest +"\n");
			
			//Destination
			System.out.print("Enter Destination e-mail message number to delete:");
			nDest=input.nextInt();
			deleteDestinationEmail(nDest);

			//copies messages from source e-mail account to destination one
			folder.open(n);
			messages = folder.getMessages();
			folder.copyMessages(messages, folderDest);
			
			
			//
			}
			} catch (MessagingException e)
	
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
	
			}//end catch
			//close the folder

			}//end Main
	
	
			public static void deleteSourceEmail(int n)
			{	//added the -1 at [n-1] because array starts at 0,without the -1 the message number would be 1 less than human logic
				message = messages[n-1];
				try {
					message.setFlag(Flags.Flag.DELETED, true);
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();}}
				
			//destination server messages
			public static void deleteDestinationEmail(int nDest)
			{
				messageDest = messagesDest[nDest-1];
				try{
					messageDest.setFlag(Flags.Flag.DELETED,true);
				} catch (MessagingException e) {
					e.printStackTrace();
					
				}
				try {
					folderDest.close(true);
				} catch (MessagingException e3) {
					// TODO Auto-generated catch block
					e3.printStackTrace();
				}
				try {
					storeDest.close();
				} catch (MessagingException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				//end destination
				
				try {
					folder.close(true);
				} catch (MessagingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					store.close();
				} catch (MessagingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		//}
	}	//end method deleteEmail
//}//end of class

Hi John, thanks for using code tags. In the first part of the code tags though, it should say CODE=Java which will put line numbers. If you do that and then tell us which line numbers are relevant (i.e. where you arrive at the end of the copying, and what line causes the Exception according to the error you are getting) that would be helpful. Thanks & welcome to Daniweb

Thank you for letting me know of the best practice with the CODE= java, which I now have done.
I think I have an intermittent problem because when I run the code down below, the error message "javax.mail.FolderClosedException" doesn't always occur, I beleive the problem is around line 135 to 137, it's where the copying of the e-mails takes place.
Line 173 to 177 is where I close the folder which is well after the copying of the messages.

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import java.util.Date;
import java.util.Scanner;

public class IMAPProject 
{
     //senders "S" , Dest is for the destination
    static String emailS;
    static String emailDest;//Destination variable
    static String passwordS;
    static String passwordDest;//Destination variable
    static String serverNameS;
    static String serverNameDest;//Destination variable
    static Session session = null;
    static Session sessionDest = null;//Destination variable
    static Store store = null;
    static Store storeDest = null;//Destination variable
    static Folder folder = null;
    static Folder folderDest = null;//Destination variable
    static Message message = null;
    static Message messageDest = null;//Destination variable
    static Message[] messages = null;
    static Message[] messagesDest = null;//Destination variable
    static String sender = null;
    static String senderDest = null;//Destination variable
    static String email = null;
    static String EmailDest = null;//Destination variable
    static String subject = null;   
    static String subjectDest = null;//Destination variable
    static int n ;
    static int nDest ;//Destination variable
    static Scanner input = new Scanner(System.in);

    //Source e-mail account
    public static void main (String args[])
    {

    System.out.print("Source e-mail account:");
    emailS=input.next();

    System.out.print("Source password:");
    passwordS=input.next();

    System.out.print("Source server name");
    serverNameS=input.next();

    //destination
    System.out.print("Destination e-mail account:");
    emailDest=input.next();

    System.out.print("Destination password:");
    passwordDest=input.next();

    System.out.print("Destination server name");
    serverNameDest=input.next();

    try {
    //establishing source connection
    session = Session.getDefaultInstance(System.getProperties(), null);
    store = session.getStore("imaps");
    store.connect(serverNameS,emailS,passwordS);

    //establishing destination connection
    sessionDest = Session.getDefaultInstance(System.getProperties(), null);
    storeDest = sessionDest.getStore("imaps");
    storeDest.connect(serverNameDest,emailDest,passwordDest);

    //establishing the source default folder
    folder = store.getDefaultFolder();
    folder = folder.getFolder("inbox");

    //establishing the destination default folder
    folderDest = storeDest.getDefaultFolder();
    folderDest = folderDest.getFolder("inbox");

    //reading the source e-mails from the inbox
    folder.open(Folder.READ_WRITE);

    //reading the destination e-mails from the inbox
    folderDest.open(Folder.READ_WRITE);

    //getting the source messages
    messages = folder.getMessages();

    //getting the destination messages
    messagesDest = folderDest.getMessages();

    for (int messageNumber = 0; messageNumber < messages.length; messageNumber++)
    {//source
        message = messages[messageNumber];


            sender = ((InternetAddress) message.getFrom()[0]).getPersonal();
            email = ((InternetAddress) message.getFrom()[0]).getAddress();
            Date date = message.getReceivedDate();
            if (sender == null)
            { 
                sender = ((InternetAddress) message.getFrom()[0]).getAddress();
            }
            System.out.print("Sender - " + sender);
            System.out.print(" email - " +email + date +"\n");

            }
            //source
            System.out.print("Enter source e-mail message number to delete:");
            n=input.nextInt();
            deleteSourceEmail(n);


            //end for source 


            //begin destination
            for (int messageNumberDest = 0; messageNumberDest < messagesDest.length; messageNumberDest++)
            {//source
            messageDest = messagesDest[messageNumberDest];
            senderDest = ((InternetAddress) messageDest.getFrom()[0]).getPersonal();
            System.out.println(senderDest);
            emailDest = ((InternetAddress) messageDest.getFrom()[0]).getAddress();
            Date dateDest = messageDest.getReceivedDate();
            if (senderDest == null)
            { 
                senderDest = ((InternetAddress) messageDest.getFrom()[0]).getAddress();
            }
            System.out.print("Sender - " + senderDest);
            System.out.print(" email - " +emailDest + dateDest +"\n");

            //Destination
            System.out.print("Enter Destination e-mail message number to delete:");
            nDest=input.nextInt();
            deleteDestinationEmail(nDest);

            //copies messages from source e-mail account to destination one
            folder.open(n);
            messages = folder.getMessages();
            folder.copyMessages(messages, folderDest);
            System.out.println("\n" + "The e-mails have been copied from the source e-mail account to the destination one." + "\n" );

            //
            }
            } catch (MessagingException e)

            {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }//end catch
            //close the folder

            }//end Main


            public static void deleteSourceEmail(int n)
            {   //added the -1 at [n-1] because array starts at 0,without the -1 the message number would be 1 less than human logic
                message = messages[n-1];
                try {
                    message.setFlag(Flags.Flag.DELETED, true);
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();}}

            //destination server messages
            public static void deleteDestinationEmail(int nDest)
            {
                messageDest = messagesDest[nDest-1];
                try{
                    messageDest.setFlag(Flags.Flag.DELETED,true);
                } catch (MessagingException e) {
                    e.printStackTrace();

                }
                try {
                    folderDest.close(true);
                } catch (MessagingException e3) {
                    // TODO Auto-generated catch block
                    e3.printStackTrace();
                }
                try {
                    storeDest.close();
                } catch (MessagingException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                //end destination

                try {
                    folder.close(true);
                } catch (MessagingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    store.close();
                } catch (MessagingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        //}
    }   //end method deleteEmail
//}//end of class 

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.