我正在开发一个应用程序,我想要获取所有邮件,无论是阅读还是未阅读。任何人都可以帮助我如何实现此功能? 我在这里使用的是IMAP,但我在这个片段中遇到了问题。
@Override
protected Void doInBackground(Void... voids) {
Properties props = new Properties();
//IMAPS protocol
props.setProperty("mail.store.protocol", "IMAP");
//Set host address
props.setProperty("mail.host", "IMAP.gmail.com");
//Set specified port
props.setProperty("mail.IMAP.port", "993");
props.setProperty("mail.transport.protocol", "IMAP");
props.setProperty("mail.imap.ssl.trust", "*");
session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc@gmail.com", "abcd");
}
});
try {
Store store = session.getStore("IMAP");
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message messages[] = inbox.search(new FlagTerm(
new Flags(Flags.Flag.SEEN), false));
for (int i = 0; i < messages.length; i++) {
Message message = messages[i]; Address[] from = message.getFrom(); System.out.println("-------------------------------");
System.out.println("Date : " + message.getSentDate());
System.out.println("From : " + from[0]);
System.out.println("Subject: " + message.getSubject());
System.out.println("Content :");
processMessageBody(message);
System.out.println("--------------------------------");
}
inbox.close(true);
store.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
public void processMessageBody(Message message) {
try { Object content = message.getContent();
// check for string
// then check for multipart
if (content instanceof String) {
System.out.println(content);
} else if (content instanceof Multipart)
{ Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
} else if (content instanceof InputStream) {
InputStream inStream = (InputStream) content;
int ch; while ((ch = inStream.read()) != -1) {
System.out.write(ch);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
} public void procesMultiPart(Multipart content) {
try {
int multiPartCount = content.getCount();
for (int i = 0; i < multiPartCount; i++) {
BodyPart bodyPart = content.getBodyPart(i);
Object o; o = bodyPart.getContent();
if (o instanceof String) {
System.out.println(o);
} else if (o instanceof Multipart) {
procesMultiPart((Multipart) o);
}
}
} catch (IOException e)
{ e.printStackTrace();
} catch (MessagingException e)
{ e.printStackTrace(); }
}`
这个代码段没有按照所需的输出工作,请让我知道我在哪里做错了它在调用message.getContent时给出异常 方法抛出'java.lang.NoClassDefFoundError'异常。