Menu

Error Purging mbox file

Help
Dave
2005-08-11
2013-05-01
  • Dave

    Dave - 2005-08-11

    hi  ben,
       Just Started Using this Api(very cool by the way) and I have the following Purging mbox file error which i cant figure out at all.

    javax.mail.MessagingException: Error purging mbox file;
      nested exception is:
            java.io.IOException: Unable to rename existing file
            at net.fortuna.mstor.MStorFolder.expunge(Unknown Source)
            at net.fortuna.mstor.MStorFolder.close(Unknown Source)
            at DeleteMessageLocalStore.DeleteMessage(DeleteMessageLocalStore.java:52

    Here is my code:
    try {

        
    URLName url = new URLName("mstor:c:/temp/mail/test/");
    Properties p = new Properties();
    p.setProperty("mstor.meta.enabled", "false");
    p.setProperty("mstor.mbox.useNioMapping", "false");
    store = new MStorStore(Session.getDefaultInstance(p), url);

    store.connect();
    Folder inbox = store.getFolder("Inbox");

    inbox.open(Folder.READ_WRITE);
    int thesize =MessageNums.size();
    for(int i=0; i <= thesize-1; i++){
        Integer emailNum = (Integer)MessageNums.elementAt(i);
         int en =emailNum.intValue();
         System.out.println("Vector Num"+en);
        Message message = inbox.getMessage(en);
        String[] avalue =message.getHeader("Message-ID");
        sdbf.DeleteEmailTrack(avalue[0]);
        message.setFlag(Flags.Flag.DELETED, true); 
       
    }

    [LINE 52]-->inbox.close(true);
    store.close();
    isDeleted = true;

    }
    catch (Exception exc) {

    By the way I do process the localFile store in other classes During Runtime but make sure that all folders are closed and stores are closed before going into this class.I keep getting this error when i get to line 52. By the way this class works perfect on its own when i tested it but when I include it with my project classes it does not work.please help! Cheers
                Dave

     
    • Ben Fortuna

      Ben Fortuna - 2005-08-11

      Hi Dave,

      This seems to be a common problem, and every time so far it has been caused by a reference holding onto the mbox file as it is being renamed.

      MboxFile.purge() will close its own file reference before the rename attempt, however there may be other references keeping the file open.

      Check for other open folders (i.e. places where you have called store.getFolder("Inbox") but haven't closed explicitly). This usually seems to be source of the problem.

      regards,
      ben

       
    • Dave

      Dave - 2005-08-12

      Hi Ben,
      Dave here again.Sorry to bother you.I solved the previous problem by putting all the Storing Reading and Deleting Operations into 1 class called LocalInboxProcessing() for other classes to call.
      Here is the code(bit Messy Sorry):

      import java.awt.*;
      import java.awt.event.*;
      import java.net.*;
      import java.util.*;
      import javax.mail.*;
      import javax.mail.internet.*;
      import javax.swing.*;
      import javax.swing.event.*;
      import net.fortuna.mstor.*;

      public class LocalInboxProcessing{
         
          private MStorStore msstore;
          private static Folder Inbox;
          private static LocalInboxProcessing theInstance = null;
          private Message[] messages;
          private int amountOfMsgs;
          private StandaloneDBFunctions sdbf = new StandaloneDBFunctions();

          private LocalInboxProcessing()
          {
              try{
              Properties p = new Properties();
              p.setProperty("mstor.meta.enabled", "false");
              p.setProperty("mstor.mbox.useNioMapping", "false");
         
              URLName url = new URLName("mstor:c:/temp/mail/test/yoke");     

              msstore = new MStorStore(Session.getDefaultInstance(p), url);
              msstore.connect();
              Inbox = msstore.getDefaultFolder().getFolder("Inbox");
              OpenFolder();
               }
               catch (Exception e) {
                e.printStackTrace();
               }

          }//end constructor

          public static LocalInboxProcessing getInstance()
          {
              if(theInstance == null)
              {
                  theInstance = new LocalInboxProcessing();
                  return theInstance;
              }
              else
              {
                  return theInstance;
              }
          }
         
          public void AddMessagesInboxFile(Message[] messages){
            try{
                    OpenFolder();
               
              Inbox.appendMessages(messages);
            }
            catch (Exception e) {
                e.printStackTrace();
               }
            }
           
          public static void CloseFolderSetToFalse(){
              try{
                  if (Inbox.isOpen()==true){
                  Inbox.close(false);
                 }
              }
              catch (Exception e) {
                e.printStackTrace();
               }
         
          }
         
          public static void CloseFolderSetToTrue(){
              try{
              if(Inbox.isOpen()==true){
              Inbox.close(true);
              }
              }
              catch (Exception e) {
                e.printStackTrace();
               }
              
          }   
          public static void OpenFolder(){
            try{
                 if (Inbox.isOpen()==false){
                Inbox.open(Folder.READ_WRITE);
              }
             
          }
          catch (Exception e) {
                e.printStackTrace();
               }
          
          }
          public static void reSetInstance(){
              try{
             
              theInstance = null;
             
              }
              catch (Exception e) {
                e.printStackTrace();
               }
         
          }
         
        public boolean DeleteMessages(Vector MessageNums){
             
              boolean isDeleted = false;
          
            try {
            if (Inbox.isOpen()==false){
                OpenFolder();
               
            }
            int thesize =MessageNums.size();
            for(int i=0; i <= thesize-1; i++){
                String Id = (String)MessageNums.elementAt(i);
               
                 for(int t=1; t <= Inbox.getMessageCount(); t++){
                    Message message = Inbox.getMessage(t);
                   
                  String[] avalue =message.getHeader("Message-ID");
                  if(Id.equals(avalue[0])){
                  sdbf.DeleteEmailTrack(avalue[0]);
                  message.setFlag(Flags.Flag.DELETED, true);
               
                    }
                  
           }
           }
            CloseFolderSetToTrue();
            //msstore.close();
          
           isDeleted = true;

      }
      catch (Exception exc) {
             exc.printStackTrace();
             isDeleted =false;
             return isDeleted;
        }
             return isDeleted;
      }
         
         
           public Message[] GetMessages(){ 
              try{
                  if (Inbox.isOpen()==false){
                    OpenFolder();
               
                }
                messages=null;
                messages = Inbox.getMessages();
                // Retrieve message headers for each message in folder.
                FetchProfile profile = new FetchProfile();
                profile.add(FetchProfile.Item.ENVELOPE);
                Inbox.fetch(messages, profile);
            
            
               
              }
              catch (Exception e) {
                e.printStackTrace();
               }
           return messages;
          }
         
            public int GetAmountOfMessages(){
              try{
                      if (Inbox.isOpen()==false){
                       OpenFolder();
               
                }
                 
                  int amountOfMsgs =Inbox.getMessageCount();

               
              
              }
              catch (Exception e) {
                e.printStackTrace();
               }
           return amountOfMsgs;
          }

      }   
         

      Still when I Perform The following calls in Order.
      Class would be Initialized**
      1.AddMessagesInboxFile(Message[] messages)
           (called a number of times) 1 message               passed in at a time.
      2.CloseFolderSetToFalse()
      3.Message[] GetMessages()
        The messages in my Inbox file dont correspond  to the messages i read.They are corrupted(Messages I read not the Inbox file).1 message could be missing or 1 message would be repeat twice.
      It works grand when I call AddMessagesInboxFile
      restart runtime(restart the whole Application)
      and then call Message[] GetMessages().
      the DeleteMessages(Vector MessageNums) function works perfect after doing this.

      Its when i add messages and then View Messages
      in the one go the messages become corrupt.I dont understand what the problem is.As u can see from my code Im slightly confused between the Inbox.close(false) and Inbox.close(true). Could do with your help Ben Thank.

       
      • Ben Fortuna

        Ben Fortuna - 2005-08-13

        Hi Dave,

        It really sounds like the problem you are having is related to the bug mentioned here:

        https://sourceforge.net/forum/forum.php?thread_id=1318732&forum_id=390660

        I have since fixed this, but haven't provided a new release yet. As mentioned in the other thread a workaround is to close the folder and open it again before reading the messages (I'm not clear if this is what you are doing already?). I haven't actually tested this workaround, but it should work..

        regards,
        ben

        ps. the boolean flag you pass to Folder.close() is to indicate whether to expunge the folder as well. So the following code should do exactly the same thing:

        option 1:
        inbox.close(true);

        option 2:
        inbox.expunge();
        inbox.close(false);

         
        • Dave

          Dave - 2005-08-13

          hi ben
          thanks for the reply.I tryed opening and closing the folder again but did not work.I went at the problem at all angles but could not figure it out.When is the new release due out if u dont mind me askn?
               it would be great to have this api bug free
               caus i think its very cool.Found no api that 
               compares to it on the net(Well Api that suited me)

                   Thanks again for the reply ben ,at least i kno now what the problem is.
                           cheers Dave

           
          • Ben Fortuna

            Ben Fortuna - 2005-08-15

            Hi Dave,

            I really thought that workaround should work, but maybe its something to do with using nio buffers..

            Anyway, considering this I'll try to release a new version within the next week or two.

            regards,
            ben

             
            • Thomas Kock

              Thomas Kock - 2005-08-19

              Hi Ben, Hi Dave!

              In my App I had some strange errors when running with properties mstor.meta.enabled=true and mstor.mbox.useNioMapping=true.
              After setting both to false, the problems disappeared - but I didnt do any further investigation. Dave, maybe in addition to re-opening, you could give this a try as well.

              my 2 cents
              Thomas

               

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.