2

I have an application which contains a GUI, it is using Javamail. When i open this Jframe I have to see messages that are sent to my mail on a jTextArea.

The problem is when i wrote my code it only shows just the last message sent.

How do I display all new messages in my inbox?

This is my code :

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

Properties props = new Properties();

props.put("mail.pop3.host", "pop.gmail.com");

props.put("mail.pop3.user", "mymail@gmail.com");

props.put("mail.pop3.socketFactory", 995);

props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.pop3.port", 995);

Session session = Session.getDefaultInstance(props, new Authenticator() {
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("mymail@gmail.com", "mypassword");

    }
});

try {
    Store store = session.getStore("pop3");

    store.connect("pop.gmail.com", "mymail@gmail.com", "mypaswword");

    Folder fldr = store.getFolder("INBOX");

    fldr.open(Folder.READ_ONLY);

    Message[] msg = fldr.getMessages();

    Address[] address;


    for (int i = 0; i < msg.length; i++) {

        jTextArea1.setText("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString());

    }

    fldr.close(true);

    store.close();

} catch (Exception e) {
    System.out.println(e);
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
wael
  • 23
  • 1
  • 3

2 Answers2

0

You repeatedly set the text of the jTextArea1 to the same contents in your loop over messages here:

for (int i = 0; i < msg.length; i++) {

    jTextArea1.setText("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString());

}

You should build a StringBuilder with all the messages and then set the contents of the jTextArea1

final StringBuilder sb = new StringBuilder();
for (int i = 0; i < msg.length; i++) {

    sb.append("SentDate : " + msg[i].getSentDate() + "\n" + "From : " + msg[i].getFrom()[0] + "\n" + "Subject : " + msg[i].getSubject() + "\n" + "Message : " + "\n" + msg[i].getContent().toString());

}
jTextArea1.setText(sb.toString());

You can then make this a lot more legible by using an enhanced for loop and using the append method of the StringBuilder.

final StringBuilder sb = new StringBuilder();
for (Message message : msg) {

    sb.append("SentDate : ").
            append(message.getSentDate()).
            append("\n").
            append("From : ").
            append(message.getFrom()[0]).
            append("\n").append("Subject : ").
            append(message.getSubject()).
            append("\n").
            append("Message : ").
            append("\n").
            append(message.getContent().toString());

}
jTextArea1.setText(sb.toString());
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Thank you very much for you response, so by using your code, it show the date and the subject of the messages but the content it shows only ths line : javax.mail.internet.MimeMultipart@ – wael Apr 09 '13 at 16:26
  • You are calling `toString` on the `message.getContent` method, but according to the [Javadoc](http://docs.oracle.com/javaee/6/api/javax/mail/Part.html#getContent()) this is not a `String` if the message is multipart. Read [this](http://stackoverflow.com/a/13013066/2071828) SO post to find out how to display a multipart message. – Boris the Spider Apr 09 '13 at 16:40
0
final StringBuilder sb = new StringBuilder();
for (Message message : msg) {

    sb.append("SentDate : ").
        append(message.getSentDate()).
        append("\n").
        append("From : ").
        append(message.getFrom()[0]).
        append("\n").append("Subject : ").
        append(message.getSubject()).
        append("\n").
        append("Message : ").
        append("\n").
        append(message.getContent().toString());

}
jTextArea1.setText(sb.toString());
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
Sadame
  • 1