Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried the following for retrieving mails to java jtextarea. It works fine but the limitation is that after retrieving all the mails in the inbox it appends it to the jtextarea and that takes plenty amount of time. My necessity is that each retrieval of mail must be shown dynamically one after other.....


Java
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.mail.*;

public class TextAreaDemoB extends JFrame 
{
JTextArea _resultArea = new JTextArea(6, 35);
      public TextAreaDemoB() throws Exception
        {
      String host = "pop.gmail.com";
      String user = "computingcloud14@gmail.com";
      String password = "QWERTYASDFG";
      Properties properties = System.getProperties();
        Session session = Session.getDefaultInstance(properties, null);
        Store store = session.getStore("pop3s");
        store.connect(host, user, password);
        Folder folder = store.getFolder("inbox");

        folder.open(Folder.READ_ONLY);
        Message[] message = folder.getMessages();

        

        
      for (int i = 0; i < message.length; i++)
      {
         String myString = Integer.toString(i);
          _resultArea.append(myString);
          _resultArea.append("------------ Message " + (i + 1) + " ------------");
          _resultArea.append("SentDate : " + message[i].getSentDate());
          _resultArea.append("From : " + message[i].getFrom()[0]);
         _resultArea.append("Subject : " + message[i].getSubject());
           InputStream stream = message[i].getInputStream();
       //The following loop must be changed to achieve my solution.can any1???????
           do
           {
               //_resultArea.setText(();
            char d=(char) stream.read();
            String c=Character.toString(d);
            _resultArea.append(c);
           
            }
           while (stream.available() != 0);
      

           
      }
        JScrollPane scrollingArea = new JScrollPane(_resultArea);
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingArea, BorderLayout.CENTER);
        this.setContentPane(content);
        this.setTitle("TReadMails");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
    }
    
    //============================================================= main
    public static void main(String[] args) throws Exception
    {
        JFrame win = new TextAreaDemoB();
        win.setVisible(true);
    }
}
Posted
Updated 27-Mar-14 4:26am
v3

1 solution

Please use a local emailserver for testing.
Gmail will ban you for 15 minutes when you've logged in. You can not make another login within that 15 min. window.
http://www.hmailserver.com/[^]
Please be aware that also this one has a ban option, that you should switch off in the settings.


If I remember correct you an simply access the message by retriving the content - unless it's HTML, which needs some special treatment.
Java
String strContent = (String)message.getContent();



Please also read here:
Fundamentals of the JavaMail API[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900