Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Read Mail From A Folder

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read Mail From A Folder

    Hello to all in this forum. I'm new in Java and try to learn through the programs that I create.
    So I try to create a programm tha reads e-mails from a local folder. My problem is that I can't open that folder.
    I don't know what I'm doing wrong. Βelow is the code of my programm.

    import java.io.*;
    import java.util.*;
    import javax.mail.*;
     
    public class MailREad {  
     
    	public static void main(String args[]) throws Exception {  
     
    		String path = "C:\\Users\\Desktop\\mail folder";
    			Folder folder =null;
     
    		folder.open(path); //this is the problem
     
    		Message[] message = folder.getMessages(); 
     
    		//mail
     
            for (int i = 0; i < message.length; i++) {
     
                System.out.println("------------ Message " + (i + 1) + " ------------");
     
                System.out.println("SentDate : " + message[i].getSentDate());
                System.out.println("From : " + message[i].getFrom()[0]);
                System.out.println("Subject : " + message[i].getSubject());
                System.out.print("Message : ");
     
                InputStream stream = message[i].getInputStream();
                while (stream.available() != 0) {
                    System.out.print((char) stream.read());
                }
                System.out.println();
            }
    	}

    Thank you in advance for your answers.
    Last edited by JavaPF; January 27th, 2011 at 08:37 AM. Reason: Please use [highlight=Java] code [/highlight] tags..


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Read Mail From A Folder

    Sorry, but saying "this is the problem" or "I can't open that folder" is as useful for us as us saying "then fix the problem" or "then open that folder differently" is useful to you.

    What do you mean by each of those things? Are you getting an Exception? Unexpected behavior? Something else? Be specific.

    But just glancing at it, you never declare your folder variable. For that matter, what is Folder? I don't see that class in the standard API.

    PS- When posting code, make sure you use the code tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Read Mail From A Folder

    Hello qwert678,

    Welcome to the Java Programming Forums.

    I have added the highlight tags around your code. For future posts, please use: [highlight=Java] code [/highlight]

    I'm not sure if you are aware but you have a missing } at the end of your code. Looking at your path, that is correct.

    I will need to import javax.mail before I can look into this any further..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Read Mail From A Folder

    OK i've imported javax.mail and the problem is:

    The method open(int) in the type Folder is not applicable for the arguments (String)

    Check these links:

    Folder (JavaMail API documentation)

    Java Mail POP3Client Example
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Read Mail From A Folder

    Another problem is that the folder variable is null. Even if the open() method is given the correct argument, it's still going to cause an NPE.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    JavaPF (January 27th, 2011)

  7. #6
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read Mail From A Folder

    Thank you all for your answers. I wonder if the is another way to open a local folder and reading e-mails?
    Thanks in advance

  8. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Read Mail From A Folder

    What type of files are in this local folder? What extension?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  9. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read Mail From A Folder

    the extensions of the files are .eml

  10. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Read Mail From A Folder

    I found this example online. See if this helps you move forward. Take note of folder.open(Folder.READ_ONLY);

        public static void main(String[] args) throws Exception {
     
            URLName server = new URLName("protocol://username:password@host/foldername");
     
            Session session = Session.getDefaultInstance(new Properties(), null);
     
            Folder folder = session.getFolder(server);
     
            if (folder == null) {
              System.out.println("Folder " + server.getFile() + " not found.");
              System.exit(1);
            }
            folder.open(Folder.READ_ONLY);
     
            // Get the messages from the server
            Message[] messages = folder.getMessages();
            for (int i = 0; i < messages.length; i++) {
              System.out.println("------------ Message " + (i + 1) + " ------------");
              messages[i].writeTo(System.out);
            }
     
            folder.close(false);
          }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Send emails from Google Mail using JavaMail API
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: February 26th, 2012, 04:21 PM
  2. java mail
    By erinbasim in forum Java SE APIs
    Replies: 2
    Last Post: May 13th, 2010, 02:17 AM
  3. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 04:13 PM
  4. Javax.Mail Query Help Me Please
    By ravjot28 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 19th, 2010, 11:06 AM
  5. Sending and Receiving mail using J2ME without server
    By chals in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: June 2nd, 2009, 09:59 AM