Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to download Email by ID?
    text
    copied!<p>I'm trying to do <strong>lazy downloading email</strong>. Basically the problem is that my emails are heavy. So I'm downloading only subject and message-id and only when it's needed I'm downloading rest of email.</p> <p>The first step is to download subject and message-id:</p> <pre><code>public void downloadInBackground(EmailAccount emailAccount, String folderName) { List&lt;Email&gt; emails = new ArrayList&lt;Email&gt;(); Message[] message = null; Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = null; Folder folder = null; try { Log.getInstance().d("xxx", "starting dowanloading"); store = session.getStore("imaps"); store.connect(emailAccount.getImapServer(), emailAccount.getUsername(), emailAccount.getPassword()); folder = store.getFolder(folderName); folder.open(Folder.READ_ONLY); message = folder.getMessages(); FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add("Subject"); fp.add("From"); fp.add("To"); fp.add("Cc"); fp.add("Bcc"); fp.add("ReplyTo"); fp.add("Date"); folder.fetch(message, fp); for (int i = 0; i &lt; message.length; i++) { Message m = message[i]; String emailId = null; m.getHeader("Message-Id"); Enumeration&lt;Header&gt; headers = m.getAllHeaders(); while (headers.hasMoreElements()) { Header h = headers.nextElement(); String mID = h.getName(); if (StringUtils.containsIgnoreCase(mID, "Message-ID")) { emailId = h.getValue(); } } if (emailId != null) { Email newMail = new LazyEmail(m, emailAccount, folderName, emailId); emails.add(newMail); } } } catch (MessagingException e) { ... } finally { if (folder != null) { folder.close(false); } if (store != null) { store.close(); } } update(emails); } </code></pre> <p>Next in LazyEmail, when it's needed. I'm downloading rest of email:</p> <pre><code>public void updateContentText() { Properties props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(props, null); Store store = null; Folder folder = null; try { store = session.getStore("imaps"); store.connect(mEmailAccount.getImapServer(), mEmailAccount.getUsername(), mEmailAccount.getPassword()); folder = store.getFolder(mFolderName); folder.open(Folder.READ_ONLY); // finding email SearchTerm searchTerm = new MessageIDTerm(mEmailId); Message[] messages = folder.search(searchTerm); assert messages.length == 1; // setting context text Part mail = messages[0]; if (mail.getContent() instanceof Multipart) { setContentText((Multipart) mail.getContent()); } else { setContentText((String) mail.getContent()); } updateState = UpdateState.CONTENT_TEXT_DOWNLOADED; } catch (IOException e) { ... } finally { if (folder != null) { folder.close(false); } if (store != null) { store.close(); } } } </code></pre> <p>Code seems looks ok. But when I'm running my Android application I gets error in line Message[] messages = folder.search(searchTerm);</p> <pre><code> javax.mail.MessagingException: A4 BAD Execute(): Error in command received; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload