private void doDeleteNotes(long accountId) throws MessagingException {
    Log.i(TAG, "doDeleteNotes() begin");
    MimeMessage msg;
    List<MimeMessage> pendingDeleteMessages = new ArrayList<MimeMessage>();
    HashMap<Long, List<Note>> folderId2NotesMap =
        buildLocalChangedFolder2NotesMap(
            Note.getLocalDeletedNotes(accountId, application.getDBHelper()));

    if (folderId2NotesMap.size() == 0) {
      Log.i(TAG, "doDeleteNotes() end... no deleted notes");
      return;
    }
    try {
      for (long folderId : folderId2NotesMap.keySet()) {
        for (Note note : folderId2NotesMap.get(folderId)) {
          msg = MessageCompose.messageFromDeletedNotes(note);
          if (loginUser != null) {
            msg.setFrom(new Address(loginUser));
          }
          msg.getMessageId();
          pendingDeleteMessages.add(msg);
        }
        Message[] delMessages =
            (Message[]) pendingDeleteMessages.toArray(new Message[pendingDeleteMessages.size()]);
        if (delMessages.length > 0) {
          if (folderId == Folder.ALL_FOLDER_ID) {
            imapStoreClient.deleteNote(delMessages, ImapConsts.IMAP__INOTES_FOLDER);
          } else {
            Folder folder = Folder.getFolderById(folderId, accountId, application.getDBHelper());
            imapStoreClient.deleteNote(delMessages, folder.name);
          }
          pendingDeleteMessages.clear();
        }
        // TODO 感觉可能有点危险,确保删除都是成功的
        for (Note note : folderId2NotesMap.get(folderId)) {
          Note.deleteNoteByIdForever(note.id, accountId, application.getDBHelper());
        }
      }
    } catch (MessagingException e) {
      Log.e(TAG, " ", e);
      throw new MessagingException(e.getLocalizedMessage());
    }
    Log.i(TAG, "doDeleteNotes() end");
  }
  private void doAddNotes(long accountId) throws MessagingException {
    Log.i(TAG, "doAddNotes() begin");
    MimeMessage msg;
    List<MimeMessage> pendingComposeMessages = new ArrayList<MimeMessage>();
    HashMap<Long, List<Note>> folderIdNotesMap =
        buildLocalChangedFolder2NotesMap(
            Note.getLocalAddedNotes(accountId, application.getDBHelper()));
    if (folderIdNotesMap.size() == 0) {
      Log.i(TAG, "doAddNotes() end ..no added notes");
      return;
    }
    try {

      for (long folderId : folderIdNotesMap.keySet()) {
        for (Note note : folderIdNotesMap.get(folderId)) {
          msg = MessageCompose.messageFromNotes(note, application);
          if (loginUser != null) {
            msg.setFrom(new Address(loginUser));
          }
          pendingComposeMessages.add(msg);
        }
        if (pendingComposeMessages.size() > 0) {
          if (folderId == Folder.ALL_FOLDER_ID) {
            imapStoreClient.createNotes(pendingComposeMessages, ImapConsts.IMAP__INOTES_FOLDER);
          } else {
            Folder folder = Folder.getFolderById(folderId, accountId, application.getDBHelper());
            imapStoreClient.createNotes(
                pendingComposeMessages,
                folder == null ? ImapConsts.IMAP__INOTES_FOLDER : folder.name);
          }
          pendingComposeMessages.clear();
        }
      }

      handleRemoteUidNullErro(accountId);

    } catch (MessagingException e) {
      Log.e(TAG, " ", e);
      throw new MessagingException(e.getLocalizedMessage());
    }
    Log.i(TAG, "doAddNotes() end");
  }
  public void testTextPlusRfc822Message() throws MessagingException {
    K9Activity.setLanguage(getContext(), "en");
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));

    String bodyText = "Some text here";
    String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment.";

    // Create text/plain body
    TextBody textBody = new TextBody(bodyText);

    // Create inner text/plain body
    TextBody innerBody = new TextBody(innerBodyText);

    // Create message/rfc822 body
    MimeMessage innerMessage = new MimeMessage();
    innerMessage.addSentDate(new Date(112, 02, 17));
    innerMessage.setRecipients(RecipientType.TO, new Address[] {new Address("*****@*****.**")});
    innerMessage.setSubject("Subject");
    innerMessage.setFrom(new Address("*****@*****.**"));
    innerMessage.setBody(innerBody);

    // Create multipart/mixed part
    MimeMultipart multipart = new MimeMultipart();
    MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822");
    bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\"");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);

    // Create message
    MimeMessage message = new MimeMessage();
    message.setBody(multipart);

    // Extract text
    ViewableContainer container = MimeUtility.extractTextAndAttachments(getContext(), message);

    String expectedText =
        bodyText
            + "\n\n"
            + "----- message.eml ------------------------------------------------------"
            + "\n\n"
            + "From: [email protected]"
            + "\n"
            + "To: [email protected]"
            + "\n"
            + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012"
            + "\n"
            + "Subject: Subject"
            + "\n"
            + "\n"
            + innerBodyText;
    String expectedHtml =
        "<html><head/><body>"
            + "<pre style=\"white-space: pre-wrap; word-wrap:break-word; "
            + "font-family: sans-serif; margin-top: 0px\">"
            + bodyText
            + "</pre>"
            + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: "
            + "1px solid #000\">message.eml</p>"
            + "<table style=\"border: 0\">"
            + "<tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">From:</th>"
            + "<td>[email protected]</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">To:</th>"
            + "<td>[email protected]</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>"
            + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>"
            + "<td>Subject</td>"
            + "</tr>"
            + "</table>"
            + "<pre style=\"white-space: pre-wrap; word-wrap:break-word; "
            + "font-family: sans-serif; margin-top: 0px\">"
            + innerBodyText
            + "</pre>"
            + "</body></html>";

    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, container.html);
  }
  @Test
  public void testTextPlusRfc822Message() throws MessagingException {
    K9ActivityCommon.setLanguage(context, "en");
    Locale.setDefault(Locale.US);
    TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));

    String innerBodyText = "Hey there. I'm inside a message/rfc822 (inline) attachment.";

    // Create text/plain body
    TextBody textBody = new TextBody(BODY_TEXT);

    // Create inner text/plain body
    TextBody innerBody = new TextBody(innerBodyText);

    // Create message/rfc822 body
    MimeMessage innerMessage = new MimeMessage();
    innerMessage.addSentDate(new Date(112, 02, 17), false);
    innerMessage.setRecipients(RecipientType.TO, new Address[] {new Address("*****@*****.**")});
    innerMessage.setSubject("Subject");
    innerMessage.setFrom(new Address("*****@*****.**"));
    MimeMessageHelper.setBody(innerMessage, innerBody);

    // Create multipart/mixed part
    MimeMultipart multipart = MimeMultipart.newInstance();
    MimeBodyPart bodyPart1 = new MimeBodyPart(textBody, "text/plain");
    MimeBodyPart bodyPart2 = new MimeBodyPart(innerMessage, "message/rfc822");
    bodyPart2.setHeader("Content-Disposition", "inline; filename=\"message.eml\"");
    multipart.addBodyPart(bodyPart1);
    multipart.addBodyPart(bodyPart2);

    // Create message
    MimeMessage message = new MimeMessage();
    MimeMessageHelper.setBody(message, multipart);

    // Extract text
    List<Part> outputNonViewableParts = new ArrayList<Part>();
    ArrayList<Viewable> outputViewableParts = new ArrayList<>();
    MessageExtractor.findViewablesAndAttachments(
        message, outputViewableParts, outputNonViewableParts);
    ViewableExtractedText container =
        messageViewInfoExtractor.extractTextFromViewables(outputViewableParts);

    String expectedText =
        BODY_TEXT
            + "\r\n\r\n"
            + "----- message.eml ------------------------------------------------------"
            + "\r\n\r\n"
            + "From: [email protected]"
            + "\r\n"
            + "To: [email protected]"
            + "\r\n"
            + "Sent: Sat Mar 17 00:00:00 GMT+01:00 2012"
            + "\r\n"
            + "Subject: Subject"
            + "\r\n"
            + "\r\n"
            + innerBodyText;
    String expectedHtml =
        "<pre class=\"k9mail\">"
            + BODY_TEXT_HTML
            + "</pre>"
            + "<p style=\"margin-top: 2.5em; margin-bottom: 1em; border-bottom: "
            + "1px solid #000\">message.eml</p>"
            + "<table style=\"border: 0\">"
            + "<tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">From:</th>"
            + "<td>[email protected]</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">To:</th>"
            + "<td>[email protected]</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">Sent:</th>"
            + "<td>Sat Mar 17 00:00:00 GMT+01:00 2012</td>"
            + "</tr><tr>"
            + "<th style=\"text-align: left; vertical-align: top;\">Subject:</th>"
            + "<td>Subject</td>"
            + "</tr>"
            + "</table>"
            + "<pre class=\"k9mail\">"
            + innerBodyText
            + "</pre>";

    assertEquals(expectedText, container.text);
    assertEquals(expectedHtml, getHtmlBodyText(container.html));
  }