private void importMessages(
      TokensAndUrlAuthData authData,
      IdempotentImportExecutor idempotentExecutor,
      Collection<MailMessageModel> messages) throws Exception {
    for (MailMessageModel mailMessageModel : messages) {
      idempotentExecutor.executeAndSwallowIOExceptions(
          mailMessageModel.toString(),
          // Trim the full mail message to try to give some context to the user but not overwhelm
          // them.
          "Mail message: " + mailMessageModel.getRawString()
              .substring(0, Math.min(50, mailMessageModel.getRawString().length())),
          () -> {
            // Gather the label ids that will be associated with this message
            ImmutableList.Builder<String> importedLabelIds = ImmutableList.builder();
            for (String exportedLabelIdOrName : mailMessageModel.getContainerIds()) {
              // By this time all the label ids have been added to tempdata
              String importedLabelId = idempotentExecutor.getCachedValue(exportedLabelIdOrName);
              if (importedLabelId != null) {
                importedLabelIds.add(exportedLabelIdOrName);
              } else {
                // TODO remove after testing
                monitor.debug(
                    () -> "labels should have been added prior to importing messages");
              }
            }
            // Create the message to import
            Message newMessage =
                new Message()
                    .setRaw(mailMessageModel.getRawString())
                    .setLabelIds(importedLabelIds.build());
            return getOrCreateGmail(authData)
                .users()
                .messages()
                .insert(USER, newMessage)
                .execute()
                .getId();
          });
    }
  }
Import each message in {@code messages} into the import account with it's associated labels.
extensions/data-transfer/portability-data-transfer-google/src/main/java/org/datatransferproject/datatransfer/google/mail/GoogleMailImporter.java