Reading all new messages from my gmail using javamail

随声附和 提交于 2019-12-01 23:22:50

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());
Sadame
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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!