这可能是愚蠢的问题,但是......
下面的代码可以完美地从 gmail 中检索所有电子邮件。但是,在运行应用程序一次后,gmail 在其“转发和 POP/IMAP”设置中设置“状态:对自 EXECUTION_APP_TIME 以来到达的所有邮件启用 POP”,并且应用程序停止接收旧邮件(这是有道理的)。
每次执行应用程序时,我都需要从 gmail 获取所有消息,但它只能工作一次。每次执行后,我都需要在我的 gmail 中将“转发和 POP/IMAP”中的配置更改为“为所有邮件启用 POP(甚至是已经下载的邮件)”。
有谁知道我该如何解决?有没有办法通过代码自动设置它。
这是源代码:
public void connect(){
Properties props = new Properties();
props.put("mail.pop3.host" , "pop.gmail.com");
props.put("mail.pop3.user" , username);
// Start SSL connection
props.put("mail.pop3.socketFactory" , 995 );
props.put("mail.pop3.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.pop3.port" , 995);
Log.e("asdasd", "teste");
Session session = Session.getDefaultInstance(props , new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( username , password);
}
});
try {
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995 , username , password);
Folder fldr = store.getFolder("Inbox");
fldr.open(Folder.HOLDS_MESSAGES);
int totalMsg = fldr.getMessageCount();
int unread = fldr.getUnreadMessageCount();
System.out.println("Total MSG: " + totalMsg + " unreaed: " + unread );
Message[] messages = fldr.getMessages();
//Message[] msg = fldr.search(new FlagTerm(new Flags(Flag.SEEN), false));
Message[] msg = messages;
System.out.print("size " + messages .length);
if (messages.length == 0) System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
System.out.println("Subject : " + messages[i].getSubject());
if(messages[i].getSubject().equals("test223")){
System.out.println("Message " + (i + 1));
System.out.println("From : " + messages[i].getFrom()[0]);
System.out.println("Subject : " + messages[i].getSubject());
Object content = messages[i].getContent();
if (content instanceof String)
System.out.print((String)content);
/* text/plain = String
* multipart" = Multipart
* MimeMessage
* input stream = Unknown Data Handler
*/
Multipart multipart = (Multipart) msg[i].getContent();
for (int x = 0; x < multipart.getCount()-1; x++) {
BodyPart bodyPart = multipart.getBodyPart(x);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
System.out.println("Mail have some attachment : ");
DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
} else {
System.out.println(bodyPart.getContent());
}
}
}
}
fldr.close(true);
store.close();
} catch(Exception exc) {
System.out.println(exc + " error");
}
}