Android 电子邮件客户端应用跟踪未读的 gmail 电子邮件
Android email client app tracking unread gmail emails
所以我一直在使用 javamail API 作为我的 android 应用程序的一部分。登录到 gmail 帐户后,用户可以编写新电子邮件、查看收件箱和已发送的邮件。在适配器 class 的帮助下,电子邮件显示在列表视图中。 (更准确地说,显示的是发件人、主题和发送日期,如果用户单击列表视图项目,那么邮件内容也会显示在新的 activity 上)。所有这些都运行良好。
我想以不同的方式显示未读电子邮件(在 gmail 客户端中也是未读的),例如如果邮件未读则将 textSyle 设置为粗体,但我在添加此功能时遇到了问题。我一直在尝试检查每封获取的电子邮件的标志,但出于某种原因,我在变量中看不到 these flags。
我用于获取邮件的代码片段(显示不在这里,在适配器 class 中):
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;
}
我在代码中添加了一些注释来解释我在标志中发现的内容。我该怎么做才能完成这项工作?我已经预测到问题了,比如我只在我的应用程序中阅读未读邮件后会发生什么,将其标志设置为可见,当我再次启动 activity 并获取邮件时,它将再次成为未读邮件,因为我没有把它们存储在本地,但在我设法找到解决方案后,这是一个问题。
感谢您的帮助!
我不清楚你是如何寻找标志的。使用 messages[i].isSet(Flags.Flag.SEEN) 将告诉您是否设置了 SEEN 标志。请注意,SEEN 标志通常会在您访问邮件内容后立即设置,因此您不必自己设置。
提示:使用InternetAddress.toUnicodeString method, or get the name and address separately using the getPersonal and getAddress方法。这将避免任何需要自己解码的情况。
所以我一直在使用 javamail API 作为我的 android 应用程序的一部分。登录到 gmail 帐户后,用户可以编写新电子邮件、查看收件箱和已发送的邮件。在适配器 class 的帮助下,电子邮件显示在列表视图中。 (更准确地说,显示的是发件人、主题和发送日期,如果用户单击列表视图项目,那么邮件内容也会显示在新的 activity 上)。所有这些都运行良好。
我想以不同的方式显示未读电子邮件(在 gmail 客户端中也是未读的),例如如果邮件未读则将 textSyle 设置为粗体,但我在添加此功能时遇到了问题。我一直在尝试检查每封获取的电子邮件的标志,但出于某种原因,我在变量中看不到 these flags。
我用于获取邮件的代码片段(显示不在这里,在适配器 class 中):
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;
}
我在代码中添加了一些注释来解释我在标志中发现的内容。我该怎么做才能完成这项工作?我已经预测到问题了,比如我只在我的应用程序中阅读未读邮件后会发生什么,将其标志设置为可见,当我再次启动 activity 并获取邮件时,它将再次成为未读邮件,因为我没有把它们存储在本地,但在我设法找到解决方案后,这是一个问题。
感谢您的帮助!
我不清楚你是如何寻找标志的。使用 messages[i].isSet(Flags.Flag.SEEN) 将告诉您是否设置了 SEEN 标志。请注意,SEEN 标志通常会在您访问邮件内容后立即设置,因此您不必自己设置。
提示:使用InternetAddress.toUnicodeString method, or get the name and address separately using the getPersonal and getAddress方法。这将避免任何需要自己解码的情况。