Ich hab ein Problem mit Gmail. Hier ist erstmal der Code:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
import javax.mail.internet.MimeMultipart;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class EmailViewer {
private Session session = null;
private Store store = null;
private Folder folder1 = null;
public void connect() throws Exception {
final String username = "..@gmail.com";
final String pw = JOptionPane.showInputDialog("pw:");
if (pw != null && !pw.isBlank()) {
Properties prop = new Properties();
prop.put("mail.imaps.host", "imap.gmail.com");
prop.put("mail.imaps.port", "993");
prop.put("mail.imaps.connectiontimeout", "5000");
prop.put("mail.imaps.timeout", "5000");
prop.put("mail.imaps.ssl.enable", "true"); // SSL
session = Session.getInstance(prop, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, pw);
}
});
store = session.getStore("imaps");
store.connect();
folder1 = store.getFolder("[Google Mail]/Alle Nachrichten");
for (int k = 0; k < 30; k++) {
System.out.println(k + ": getMessageCount()");
folder1.open(Folder.READ_WRITE);
int messageCount = folder1.getMessageCount();
for (int i = messageCount - 100; i < messageCount; i++) {
Message m = folder1.getMessage(i);
if (!m.getFlags().toString().contains("Seen")) {
StringBuilder b = new StringBuilder();
b.append("Message: " + i + "\n");
b.append("----\n");
ArrayList<Header> list = Collections.list(m.getAllHeaders());
for (int j = 0; j < list.size(); j++) {
Header h = list.get(j);
b.append(h.getName() + ": " + h.getValue() + "\n");
}
b.append("----\n");
b.append(getText(m) + "\n");
JFrame jf = new JFrame();
JTextArea jta = new JTextArea(b.toString());
jf.add(new JScrollPane(jta));
jf.setSize(650, 650);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jf.setVisible(true);
}
}
folder1.close();
Thread.sleep(30 * 1000);
}
store.close();
/*
* Alternative!
*/
// Add messageCountListener to listen for new messages
// folder1.addMessageCountListener(new MessageCountAdapter() {
// public void messagesAdded(MessageCountEvent ev) {
// try {
// Message[] msgs = ev.getMessages();
// for (int i = 0; i < msgs.length; i++) {
// Message m = msgs[i];
// StringBuilder b = new StringBuilder();
// b.append("Message: " + i + "\n");
// b.append("----\n");
// ArrayList<Header> list = Collections.list(m.getAllHeaders());
// for (int j = 0; j < list.size(); j++) {
// b.append(list.get(i).toString() + "\n");
// }
// b.append("----\n");
// b.append(getText(m) + "\n");
//
// JFrame jf = new JFrame();
// JTextArea jta = new JTextArea(b.toString());
// jf.add(new JScrollPane(jta));
// jf.setSize(650, 650);
// jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// jf.setVisible(true);
// }
// } catch (IOException | MessagingException e) {
// e.printStackTrace();
// }
// }
// });
// System.out.println("Listen for new messages...");
}
}
private String getText(Message m) throws IOException, MessagingException {
if (m.isMimeType("text/plain")) {
return m.getContent().toString();
} else if (m.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart) m.getContent();
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
return bodyPart.getContent().toString();
} else if (bodyPart.isMimeType("text/html")) {
return bodyPart.getContent().toString();
} else if (bodyPart.getContent() instanceof MimeMultipart) {
return ((MimeMultipart) bodyPart.getContent()).getBodyPart(0).getContent().toString();
}
}
}
return "empty";
}
public static void main(String[] args) throws Exception {
EmailViewer ev = new EmailViewer();
ev.connect();
}
}
Wenn eine neue E-Mail eingeht, dann soll sich ein Fenster öffnen… Das Problem ist aber, dass .getMessageCount();
immer 1000 zurückgibt, und deshalb der .addMessageCountListener()
nicht funktioniert (Gmail gibt immer die neusten 1000 zurück). Aber die Schleife for (int i = messageCount - 100; i < messageCount; i++) {
funktioniert auch nicht richtig; wenn ich die Anwendung starte und mir eine E-Mail sende, so erscheint kein Fenster. Habt ihr Rat?