Giter Club home page Giter Club logo

mail-importer's Introduction

Mail Importer for Gmail

Do you have an old Thunderbird mail archive that you POP3ed down from AOL?

Do you want to move those old messages to Gmail so that you can use the Gmail app on your phone and still have access to everything?

If so, maybe Mail Importer for Gmail is for you!

If you are trying to bulk import mbox format files to Google Workspace, you should probably look at import-mailbox-to-gmail.

DISCLAIMER: This is not an official Google product.

What does it do?

Mail Importer for Gmail will upload the contents of a Thunderbird mail archive to Gmail and do its best to preserve the read state, flagged state, and folders of the messages. As messages are uploaded verbatim, Gmail will have an exact copy, including all attachments and headers.

Mail Importer also makes sure to only upload messages that aren't already in Gmail. This makes it easy to re-run the import multiple times if something goes wrong.

How can I run it?

Currently, Mail Importer for Gmail is in early development. It is not user friendly in any way. If you are not a developer, you probably want to stay away.

Getting a Client Secret

Each developer needs a client secret to identify their version of Mail Importer to Google using OAuth2. To get a client secret, you have to create a project in the Google Developers Console, configure it correctly, then download the generated credentials. The result should be a JSON file named something like:

client_secret_729820383-898athoe9t33ntohuoc.apps.googleusercontent.com.json

You need to copy this to src/main/resources/client_secret.json in your Mail Importer project. Never check in this file! It is your key and no one else's.

Building Mail Importer

Mail Importer uses Maven. This means that it will download all of the necessary dependencies automatically when you build it like this:

mvn clean package assembly:single

This will produce a runnable .jar file in target/mail-importer-1.0-SNAPSHOT-jar-with-dependencies.jar.

Running Mail Importer

Once Mail Importer is built, you can run it like:

java -jar ./target/mail-importer-1.0-SNAPSHOT-jar-with-dependencies.jar \
    --mailbox DIRECTORY

where DIRECTORY is the Thunderbird mailbox to open.

Note that the Thunderbird Mail directory usually has several sub-directories called ImapMail, OfflineCache, and Mail. Then under Mail, you should find individual accounts, like pop.mail.yahoo.com or pop.csi.com, and Local Folders. It is this last level of directory that contains the actual mailbox.

For example, if you had old CompuServe mail on a Mac, you might run Mail Importer like this:

java -jar ./target/mail-importer-1.0-SNAPSHOT-jar-with-dependencies.jar \
    --mailbox /Users/me/Library/Thunderbird/my_profile/Mail/pop.csi.com

Can I help make Mail Importer better?

You bet! See the CONTRIBUTING.md file for more information.

mail-importer's People

Contributors

eesheesh avatar iflan avatar ralfharing avatar sethoscope avatar xylo04 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mail-importer's Issues

NullPointerException while apparently fetching inbox categories

My mail-importer runs are now stuck on one specific piece of mail which causes the following output:

For <1255033550.1235275168730.JavaMail.SYSTEM@rc-v3conta015> got:
  message id: 11f9c233866addb7

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f010d1316a5c12"
}

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f010f2a3c7984d"
}

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f010f2a1b7b7a7"
}

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f010f2f228602d"
}

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f5e544cf682d10"
}

{MEDIA_COMPLETE] Progress: 100
  "id" : "11f5eb6cfbe92765"
}
For message 11eff470d2933775, got labels: CATEGORY_PERSONAL
For message 11f9c233866addb7, got labels: IMPORTANT, CATEGORY_UPDATES
For message 162658c42c8b8c96, got labels: CATEGORY_UPDATES
For message 11f5eb75579e0d41, got labels: CATEGORY_PERSONAL
For message 11f15c83b419501a, got labels: CATEGORY_UPDATES
For message 162658bc7737bec4, got labels: CATEGORY_UPDATES
For message 162658de11dfd901, got labels: CATEGORY_UPDATES
For message 11d8aaa4d9548aa7, got labels: CATEGORY_UPDATES
For message 11ea8a506f749639, got labels: CATEGORY_UPDATES
For message 11f15c188984fea7, got labels: CATEGORY_UPDATES
For message 1174a843540f3c2b, got labels: CATEGORY_PERSONAL
For message 11f5e71c01c485af, got labels: CATEGORY_UPDATES
For message 162658ba3c441702, got labels: CATEGORY_UPDATES
For message 116a80ac6c7040d7, got labels: CATEGORY_PERSONAL
For message 162658c28350473d, got labels: CATEGORY_UPDATES
For message 11f01f6fd33822ea, got labels: IMPORTANT, CATEGORY_UPDATES
For message 162658c5230d63cf, got labels: CATEGORY_UPDATES
For message 11f32d0ec4dd5149, got labels: IMPORTANT, CATEGORY_PERSONAL
For message 11eae5dd6a5e976b, got labels: CATEGORY_UPDATES
For message 162658bcfe0b102e, got labels: CATEGORY_UPDATES
For message 116683f48143041a, got labels: CATEGORY_UPDATES
For message 162658b85b5176f2, got labels: CATEGORY_UPDATES
For message 11f5eb6d64922a4e, got labels: CATEGORY_UPDATES
For message 11f50eeb8da70c9a, got labels: CATEGORY_PERSONAL
For message 11f006ef64257dbf, got labels: CATEGORY_UPDATES
For message 11eff470d35a2e1b, got labels: CATEGORY_PERSONAL
For message 116afb91a764d6b1, got labels: IMPORTANT, CATEGORY_UPDATES
For message 162658baf66f9115, got labels: CATEGORY_UPDATES
For message 11ea51fe6e9895db, got labels: CATEGORY_UPDATES
For message 116cc34c394ada9c, got labels: CATEGORY_PERSONAL
For message 162658bb6891fd85, got labels: CATEGORY_UPDATES
For message 162658624717564b, got labels: CATEGORY_UPDATES
For message 162658b895e56e4f, got labels: CATEGORY_UPDATES
For message 162658decb5ab72a, got labels: CATEGORY_UPDATES
For message 116bb5bafdb405a8, got labels: CATEGORY_UPDATES
For message 11f010f2a3c7984d, got labels: CATEGORY_UPDATES
For message 11f5e544cf682d10, got labels: CATEGORY_UPDATES
For message 11f25f2204d9ab04, got labels: CATEGORY_UPDATES
For message 1173739d3e63783b, got labels: CATEGORY_PERSONAL
For message 116a46bb7b9aeb9f, got labels: CATEGORY_UPDATES
For message 11f33540edb1e109, got labels: CATEGORY_PERSONAL
For message 116a63c60e4183da, got labels: CATEGORY_PERSONAL
For message 11ea5206610cede3, got labels: CATEGORY_UPDATES
For message 11f5eb6cfbe92765, got labels: CATEGORY_UPDATES
For message 162658dd3fb94e6f, got labels: CATEGORY_UPDATES
For message 16265865063f5cbf, got labels: CATEGORY_UPDATES
For message 116666340f80c6d4, got labels: CATEGORY_UPDATES
For message 116bb5c236cf11e6, got labels: CATEGORY_UPDATES
For message 116bdea4c92634a7, got labels: CATEGORY_UPDATES
For message 11f5164d759ffe81, got labels: CATEGORY_PERSONAL
For message 11f9bbff279449d3, got labels: CATEGORY_PERSONAL
For message 1626585cf73ae673, got labels: CATEGORY_UPDATES
For message 11d93d23beffc53e, got labels: CATEGORY_PERSONAL
For message 162658b944115e92, got labels: CATEGORY_UPDATES
For message 11f010f2a1b7b7a7, got labels: CATEGORY_UPDATES
For message 1173bdd276aad775, got labels: IMPORTANT, CATEGORY_UPDATES
For message 1626585a09f1e1ee, got labels: CATEGORY_UPDATES
For message 116a86d943a728b1, got labels: CATEGORY_PERSONAL
For message 116ced31a39a28b2, got labels: CATEGORY_PERSONAL
For message 11d93ae186ab465c, got labels: CATEGORY_PERSONAL
For message 1173791c7b0b524f, got labels: CATEGORY_PERSONAL
For message 116afbbec08e1de9, got labels: CATEGORY_UPDATES
For message 1626585b89dcc1dc, got labels: CATEGORY_UPDATES
For message 11f61129bdbdf954, got labels: CATEGORY_UPDATES
For message 11f5eb6a9d88ba60, got labels: CATEGORY_UPDATES
For message 11f96f4694e719fc, got labels: IMPORTANT, CATEGORY_UPDATES
For message 11f010d1316a5c12, got labels: CATEGORY_UPDATES
For message 162658be8eb58592, got labels: CATEGORY_UPDATES
For message 11eae42a1f93d6fc, got labels: CATEGORY_UPDATES
For message 162658c305680dd9, got labels: CATEGORY_UPDATES
For message 11eff470ce29a3b3, got labels: CATEGORY_PERSONAL
For message 1626585beb912e12, got labels: CATEGORY_UPDATES
For message 11e6a897ccafb741, got labels: CATEGORY_UPDATES
For message 11f04ba10f74dfb2, got labels: CATEGORY_PERSONAL
For message 11d8c334ff5a3f48, got labels: IMPORTANT, CATEGORY_UPDATES
For message 162658bdab673eeb, got labels: CATEGORY_UPDATES
For message 162658b619aade87, got labels: CATEGORY_UPDATES
For message 162658c088c27044, got labels: CATEGORY_UPDATES
For message 11e8902fcfb1f008, got labels: CATEGORY_UPDATES
For message 1626586cc35c51aa, got labels: CATEGORY_UPDATES
For message 116f20d9f3fbe234, got labels: CATEGORY_UPDATES
For message 11effd48aa9be645, got labels: CATEGORY_PERSONAL
For message 11f1eda07977428b, got labels: CATEGORY_UPDATES
For message 11f010f2f228602d, got labels: CATEGORY_UPDATES
For message 1626585f71a6f89a, got labels: CATEGORY_UPDATES
For message 11f03cef390405ca, got labels: IMPORTANT, CATEGORY_PERSONAL
For message 162658b798d600c2, got labels: CATEGORY_UPDATES
For message 11f18cc301b3fa69, got labels: CATEGORY_PERSONAL
For message 162658618f14d387, got labels: CATEGORY_UPDATES
For message 162658c1ca9ea89c, got labels: CATEGORY_UPDATES
For message 1626586401d46859, got labels: CATEGORY_UPDATES
For message 11f3f51a04cfde77, got labels: CATEGORY_UPDATES
For message 11d9248fdb8c63bd, got labels: CATEGORY_PERSONAL
For message 162658b9ad9c5c93, got labels: CATEGORY_UPDATES
For message 116ae9a67499498d, got labels: CATEGORY_UPDATES
For message 11f5e8be6ecafaf9, got labels: IMPORTANT, CATEGORY_UPDATES
For message 116c9b71547c0acb, got labels: CATEGORY_UPDATES
For message 1626585ac96c3706, got labels: CATEGORY_UPDATES
For message 116cf7ddb2163677, got labels: CATEGORY_UPDATES
For message 11f1fd98414f9a5c, got labels: CATEGORY_UPDATES
Exception in thread "main" java.lang.NullPointerException
        at to.lean.tools.gmail.importer.gmail.Mailbox.lambda$syncLocalLabelsToGmail$3(Mailbox.java:288)
        at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
        at java.util.HashMap$KeySpliterator.forEachRemaining(Unknown Source)
        at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
        at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
        at java.util.stream.ReferencePipeline.collect(Unknown Source)
        at to.lean.tools.gmail.importer.gmail.Mailbox.syncLocalLabelsToGmail(Mailbox.java:289)
        at to.lean.tools.gmail.importer.gmail.GmailSyncer.sync(GmailSyncer.java:82)
        at to.lean.tools.gmail.importer.Importer.importMail(Importer.java:116)
        at to.lean.tools.gmail.importer.Importer.main(Importer.java:82)

Silent failure while trying to upload?

$ java -jar ./target/mail-importer-0.0.0-SNAPSHOT-jar-with-dependencies.jar --mailbox /home/hiren/email_archive/Maildir/nw/beastie/

I get

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

followed by the dump of my gmail mailbox labels etc and the command exits.

what am I missing?

Problem with client secret

% java -jar ./target/mail-importer-0.0.0-SNAPSHOT-jar-with-dependencies.jar --mailbox /tmp/export/forum.mbox/
Exception in thread "main" com.google.inject.ProvisionException: Unable to provision, see the following errors:

1) Error in custom provider, java.lang.IllegalArgumentException: 
  while locating to.lean.tools.gmail.importer.gmail.Authorizer
  at to.lean.tools.gmail.importer.gmail.GmailServiceModule.configure(GmailServiceModule.java:43)
  while locating com.google.api.client.auth.oauth2.Credential
    for parameter 1 at to.lean.tools.gmail.importer.gmail.GmailService.<init>(GmailService.java:46)
  at to.lean.tools.gmail.importer.gmail.GmailServiceModule.configure(GmailServiceModule.java:42)
  while locating to.lean.tools.gmail.importer.gmail.GmailService
    for parameter 0 at to.lean.tools.gmail.importer.gmail.Mailbox.<init>(Mailbox.java:73)
  while locating to.lean.tools.gmail.importer.gmail.Mailbox
    for parameter 0 at to.lean.tools.gmail.importer.gmail.GmailSyncer.<init>(GmailSyncer.java:39)
  while locating to.lean.tools.gmail.importer.gmail.GmailSyncer
    for parameter 2 at to.lean.tools.gmail.importer.Importer.<init>(Importer.java:90)
  while locating to.lean.tools.gmail.importer.Importer

1 error
	at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025)
	at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
	at to.lean.tools.gmail.importer.Importer.main(Importer.java:81)
Caused by: java.lang.IllegalArgumentException: 
	at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:880)
	at com.google.api.client.json.JsonParser.parse(JsonParser.java:381)
	at com.google.api.client.json.JsonParser.parse(JsonParser.java:335)
	at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:165)
	at com.google.api.client.json.JsonParser.parseAndClose(JsonParser.java:147)
	at com.google.api.client.json.JsonFactory.fromReader(JsonFactory.java:236)
	at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.load(GoogleClientSecrets.java:192)
	at to.lean.tools.gmail.importer.gmail.Authorizer.loadGoogleClientSecrets(Authorizer.java:131)
	at to.lean.tools.gmail.importer.gmail.Authorizer.get(Authorizer.java:66)
	at to.lean.tools.gmail.importer.gmail.Authorizer.get(Authorizer.java:49)

Caused by: java.lang.IllegalArgumentException: expected numeric type but got class com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets
	at com.google.api.client.json.JsonParser.parseValue(JsonParser.java:843)

I have put my client secret in a file called client_secret.json, but the instructions are a bit unclear - should the filename actually be the key?

The result should be a JSON file named something like:

client_secret_729820383-898athoe9t33ntohuoc.apps.googleusercontent.com.json

That is, is client_secret.json the name of the directory rather than the file?

I guess the prefix client_secret should be replaced with some random characters?

My client secret looks like this:

13 digits dash 32 mixed digits and letters followed by .apps.googleusercontent.com

but that one is labelled "client ID" in the Google Console while the "client secret" is a mix of 24 digits and characters with no suffix (like in your example and my client ID).

Can you clarify?

Thank you

Failure when messageID is missing

I'm trying to import some folders from thunderbird and run into issues where the messageid header is missing from the mail.

This results in a null pointer exception:

Exception in thread "main" java.lang.NullPointerException
    at     to.lean.tools.gmail.importer.local.thunderbird.ThunderbirdLocalMessage.getMessageId(ThunderbirdLocalMessage.java:58)
    at to.lean.tools.gmail.importer.gmail.Mailbox.mapMessageIds(Mailbox.java:112)
    at to.lean.tools.gmail.importer.gmail.GmailSyncer.sync(GmailSyncer.java:70)
    at to.lean.tools.gmail.importer.Importer.importMail(Importer.java:118)
    at to.lean.tools.gmail.importer.Importer.main(Importer.java:83)

Here is an example of a message with the issues:

From - Fri May 14 18:21:23 2004
X-UIDL: 1084486634.2233.mail-07
X-Mozilla-Status: 0001
X-Mozilla-Status2: 00000000
Return-Path: <orders@xxxxx>
Delivered-To: iain@xxxxxxxx
X-Envelope-To: <iain@xxxxxxx>
Received: (qmail 2198 invoked from network); 13 May 2004 22:17:13 -0000
Received: from unknown (HELO dcg.neowing.co.jp) (202.234.167.50)
  by mail.iinet.net.au with SMTP; 13 May 2004 22:17:12 -0000
Received: (qmail 31718 invoked from network); 13 May 2004 22:17:08 -0000
Received: from unknown (HELO dopey) (192.168.10.66)
  by 192.168.10.1 with SMTP; 13 May 2004 22:17:08 -0000
From: orders@xxxxx
To: iain@xxxxxxx
Cc: order@xxxxx
Subject: Order Confirmation #10100255047
MIME-Version: 1.0
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: ZOLAR 6.0i


Dear Iain Riley,

Thank you for placing your order from cdjapan.co.jp.

Thoughts on how to re-generate messageid's? I was thinking about some sort of MD5/BASE64 hash based on date/from/to?

I am getting the error while implementing on Android :java.security.KeyStoreException: JKS not found

have integrated this code in android .
i get error like this

java.security.KeyStoreException: JKS not found
2019-05-31 17:36:01.065 29980-29980/com.netwin.readmails W/System.err:     at java.security.KeyStore.getInstance(KeyStore.java:890)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.util.SecurityUtils.getJavaKeyStore(SecurityUtils.java:53)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.googleapis.GoogleUtils.getCertificateTrustStore(GoogleUtils.java:74)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(GoogleNetHttpTransport.java:55)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.netwin.readmails.MainActivity.getEmails(MainActivity.java:90)
2019-05-31 17:36:01.066 29980-29980/com.netwin.readmails W/System.err:     at com.netwin.readmails.MainActivity.onCreate(MainActivity.java:82)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.Activity.performCreate(Activity.java:7003)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.-wrap11(Unknown Source:0)
2019-05-31 17:36:01.067 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:105)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.os.Looper.loop(Looper.java:164)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6798)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
2019-05-31 17:36:01.068 29980-29980/com.netwin.readmails W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err: Caused by: java.security.NoSuchAlgorithmException: JKS KeyStore not available
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err:     at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
2019-05-31 17:36:01.069 29980-29980/com.netwin.readmails W/System.err:     at java.security.Security.getImpl(Security.java:590)
2019-05-31 17:36:01.070 29980-29980/com.netwin.readmails W/System.err:     at java.security.KeyStore.getInstance(KeyStore.java:887)
2019-05-31 17:36:01.070 29980-29980/com.netwin.readmails W/System.err: 	... 17 more

here is my code

import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.Label;
import com.google.api.services.gmail.model.ListLabelsResponse;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;


public class MainActivity extends AppCompatActivity{
    private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens";

    TextView tvEmail;

    /**
     * Global instance of the scopes required by this quickstart.
     * If modifying these scopes, delete your previously saved tokens/ folder.
     */
    private static final List<String> SCOPES = Collections.singletonList(GmailScopes.GMAIL_LABELS);
    private static  String CREDENTIALS_FILE_PATH = "";

    /**
     * Creates an authorized Credential object.
     * @param HTTP_TRANSPORT The network HTTP Transport.
     * @return An authorized Credential object.
     * @throws IOException If the credentials.json file cannot be found.
     */

    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        // Load client secrets.

        Uri path = Uri.parse("file:///android_asset/credentials.json");

        String newPath = path.toString();
        CREDENTIALS_FILE_PATH = newPath;

        InputStream in = MainActivity.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvEmail = findViewById(R.id.tv_emails);
        getEmails();

    }


    private void getEmails(){
        final NetHttpTransport HTTP_TRANSPORT;
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                    .setApplicationName(APPLICATION_NAME)
                    .build();

            // Print the labels in the user's account.
            String user = "me";
            ListLabelsResponse listResponse = service.users().labels().list(user).execute();
            List<Label> labels = listResponse.getLabels();
            if (labels.isEmpty()) {
                System.out.println("No labels found.");
            } else {
                System.out.println("Labels:");
                for (Label label : labels) {
                    System.out.printf("- %s\n", label.getName());
                }
            }
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}

Exception in thread "main" java.lang.IllegalArgumentException: no JSON input found

I'm getting this error for all Thunderbird mail folders I have. This occurs after the labels JSON is printed to stdout:

Exception in thread "main" java.lang.IllegalArgumentException: no JSON input found at com.google.common.base.Preconditions.checkArgument(Preconditions.java:145) at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:47) at com.google.api.client.json.JsonParser.startParsing(JsonParser.java:215) at com.google.api.client.json.JsonParser.parse(JsonParser.java:358) at com.google.api.client.json.JsonParser.parse(JsonParser.java:335) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:79) at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:73) at com.google.api.client.googleapis.batch.BatchUnparsedResponse.getParsedDataClass(BatchUnparsedResponse.java:223) at com.google.api.client.googleapis.batch.BatchUnparsedResponse.parseAndCallback(BatchUnparsedResponse.java:184) at com.google.api.client.googleapis.batch.BatchUnparsedResponse.parseNextResponse(BatchUnparsedResponse.java:149) at com.google.api.client.googleapis.batch.BatchRequest.execute(BatchRequest.java:269) at to.lean.tools.gmail.importer.gmail.Mailbox.mapMessageIds(Mailbox.java:134) at to.lean.tools.gmail.importer.gmail.GmailSyncer.sync(GmailSyncer.java:66) at to.lean.tools.gmail.importer.Importer.importMail(Importer.java:108) at to.lean.tools.gmail.importer.Importer.main(Importer.java:76)
Update: remove my debugging, compile and run so that the line numbers make sense.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.