所以我一直在使用javamail API作为我的Android应用程序的一部分。登录到Gmail帐户后,用户可以编写新电子邮件,检查收件箱并发送邮件。在适配器类的帮助下,电子邮件显示在列表视图中。 (更准确地说,显示发件人,主题和发送日期,如果用户点击列表视图项,那么邮件内容也将显示在新活动上)。这一切都运作良好。
我希望以不同的方式显示未读电子邮件(在gmail客户端中也未读取),例如如果邮件未读,请将textSyle设置为粗体,但我在添加此功能时遇到问题。我一直在尝试检查每个提取的电子邮件的标记,但由于某种原因,我在变量中看不到nprogress。
我的代码片段用于获取邮件(显示不在此处,在适配器类中):
protected ArrayList<Email_Message> doInBackground(Void... args) {
try {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("imaps");
store.connect("imap.gmail.com", username, password);
// create the folder object and open it
Folder emailFolder = store.getFolder("INBOX");
emailFolder.open(Folder.READ_WRITE);
Flags flags2 = emailFolder.getPermanentFlags(); //has 2 weird user flags in it ($phishing, $notphising) and systemflags = -2147483061 ???
Flags seen = new Flags(Flags.Flag.RECENT);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message messages2[] = emailFolder.search(unseenFlagTerm); //this will net the same result as getMessages(), only here for testing
int test = emailFolder.getUnreadMessageCount(); //as far as i can tell this is working (i have 5000+ emails and 37 them are unread somewhere) but when i get a new email and the number goes up by 1 (38), and if i run the code again, after i already fetched the mails once, it's gonna be 37 again, and the mail marked as read in my gmail webclient too
// retrieve the messages from the folder in an array and print it
Message[] messages = emailFolder.getMessages();
int j = messages.length - 1;
for (int i = j - startIndex; i > j - startIndex - offset && i > (-1); i--) { //startIndex and offset are for displaying only 10 messages at the start and loading another 10 if the user scrolls to bottom
if (isCancelled()){
break;
}
Email_Message mailMessage = new Email_Message(); //my class for storing email messages
mailMessage.messageType = 1;
//some tricks to get the address in the right format
Address[] email_address = messages[i].getFrom();
String tempAddress = email_address[0].toString();
tempAddress = MimeUtility.decodeText(tempAddress);
//still tempering with address, not important
if(tempAddress.contains("=?")){
String[] AddressParts = tempAddress.split("\\?=");
mailMessage.messageAddress = AddressParts[1].substring(2);
}
else {
mailMessage.messageAddress = tempAddress;
}
Flags flags = messages[i].getFlags(); //user_flags = null, system_flags = 32
Flags.Flag[] systemflags = flags.getSystemFlags(); //has 1 item in it: bit = 32
String str[]= flags.getUserFlags(); //empty, these are all true for all my mails, not just one
mailMessage.messageDate = messages[i].getSentDate().toString();
mailMessage.messageSubject = messages[i].getSubject();
Object msgContent = messages[i].getContent();
String content = ""; //getting the content of the mail with these multipart stuffs
if (msgContent instanceof Multipart) {
Multipart multipart = (Multipart) msgContent;
Log.e("BodyPart", "MultiPartCount: " + multipart.getCount());
for (int k = 0; k < multipart.getCount(); k++) {
BodyPart bodyPart = multipart.getBodyPart(k);
String disposition = bodyPart.getDisposition();
if (disposition != null && (disposition.equalsIgnoreCase("ATTACHMENT"))) {
DataHandler handler = bodyPart.getDataHandler();
content = handler.getName();
} else {
content = bodyPart.getContent().toString();
}
}
} else
content = messages[i].getContent().toString();
mailMessage.messageContent = content;
EmailInbox.add(mailMessage);
}
// close the store and folder objects
emailFolder.close(false);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return EmailInbox;
}
我在代码中加了一些注释来解释我在旗帜中发现了什么。我能做些什么来完成这项工作?我已经预测到了问题,例如我在我的应用程序中读取未读邮件后发生的情况,将其标记设置为已显示,当我再次启动活动并获取邮件时,它将再次被读取,因为我不会#39; t将它们存储在本地,但在我首先找到解决方案后,这是一个问题。
感谢您的帮助!
答案 0 :(得分:1)
我不清楚你是如何寻找旗帜的。使用消息[i] .isSet(Flags.Flag.SEEN)将告诉您是否已设置SEEN标志。请注意,一旦您访问邮件内容,通常会设置SEEN标志,因此您不必自己设置。
提示:使用InternetAddress.toUnicodeString方法,或使用getPersonal和getAddress方法分别获取名称和地址。这将避免任何需要自己解码它们。