Hallo,
ich bin etwas am verzweifeln, Suche im Internet (google^^) hat mich auch nicht weiter gebracht.
Es geht darum, z. B. alle 10 Minuten (ich denke, das ist nicht unfair) auf neue Mails zu prüfen,
und dabei ganz wenig / am wenigsten Traffic zu verbrauchen.
Leider ist getNewMessageCount() und getUnreadMessageCount() nicht ganz so toll dokumentiert,
und leider ist das wohl implementationsabhängig von JavaMail API sowie Gmail (API):
https://javamail.java.net/nonav/docs/api/javax/mail/Folder.html
getNewMessageCount public int getNewMessageCount() throws MessagingException Get the number of new messages in this Folder. This method can be invoked on a closed folder. However, note that for some folder implementations, getting the new message count can be an expensive operation involving actually opening the folder. In such cases, a provider can choose not to support this functionality in the closed state, in which case this method must return -1. Clients invoking this method on a closed folder must be aware that this is a potentially expensive operation. Clients must also be prepared to handle a return value of -1 in this case. This implementation returns -1 if this folder is closed. Else this implementation gets each Message in the folder using getMessage(int) and checks whether its RECENT flag is set. The total number of messages that have this flag set is returned. Returns: number of new messages. -1 may be returned by certain implementations if this method is invoked on a closed folder. Throws: FolderNotFoundException - if this folder does not exist. MessagingException - for other failures
— getUnreadMessageCount() quasi analog.
Ok, ihr möchtet (Quelltext) sehen: Folgender Versuch funktioniert NICHT:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.imap.ssl.trust", "imap.gmail.com");
Session session = Session.getDefaultInstance(props);
store = session.getStore();
store.connect("imap.gmail.com", 993, "user name here@gmail.com", "strong password");
folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
}```
``` private static int empfangen() throws IOException, MessagingException {
int sum = 0;
int unreadMessageCount = folder.getUnreadMessageCount();
System.out.println("unreadMessageCount = " + unreadMessageCount);
if (unreadMessageCount > 0) {
Message[] messages = folder.getMessages();
for (Message message : messages) {
boolean seen = message.getFlags().contains(Flags.Flag.SEEN);
if (!seen) {
String from = message.getFrom()[0].toString();
if (from.contains("hallo")) {
// do something magic...
sum++;
}
message.setFlag(Flags.Flag.SEEN, true);
}
}
}
return sum;
}```
(``` private static void delete() throws MessagingException {
folder.close(true);
store.close();
}```)
Folgender Versuch FUNKTIONIERT (aber suboptimal, dazu später mehr):
``` private static void init() throws MessagingException {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.imap.ssl.trust", "imap.gmail.com");
Session session = Session.getDefaultInstance(props);
store = session.getStore();
store.connect("imap.gmail.com", 993, "user name here@gmail.com", "strong password");
}```
``` private static int empfangen() throws IOException, MessagingException {
int sum = 0;
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
int unreadMessageCount = folder.getUnreadMessageCount();
System.out.println("unreadMessageCount = " + unreadMessageCount);
if (unreadMessageCount > 0) {
Message[] messages = folder.getMessages();
for (Message message : messages) {
boolean seen = message.getFlags().contains(Flags.Flag.SEEN);
if (!seen) {
String from = message.getFrom()[0].toString();
if (from.contains("hallo")) {
// do something magic...
sum++;
}
message.setFlag(Flags.Flag.SEEN, true);
}
}
}
folder.close(true);
return sum;
}```
(``` private static void delete() throws MessagingException {
store.close();
}```)
Was ist jetzt der Unterschied?:
(- session bleibt bestehen,)
- folder muss jedes Mal neu geöffnet werden, das könnte Traffic verbrauchen!!!!
Könnt ihr mir sagen, ob man das allgemein so machen würd, und ob folder jedes Mal neu geöffnet werden muss?
Dann noch einen Teil der Imports, falls WICHTIG:
[spoiler]```import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;```[/spoiler]
Edit: Ich hoffe, ich hab NICHT irgendwo eine Mailadresse oder ein Password mitkopiert... (das wäre Schrecklich...)