All Projects → spajus → gmail4j

spajus / gmail4j

Licence: Apache-2.0 License
Gmail API for Java

Programming Languages

java
68154 projects - #9 most used programming language

Gmail4J - Gmail API for Java

Gmail4J is a simple, object oriented library for accessing Gmail services from Java.

Use with Maven

Add the dependency to your pom.xml:

<dependency>
  <groupId>com.googlecode.gmail4j</groupId>
  <artifactId>gmail4j</artifactId>
  <version>0.4</version>
</dependency>

Or use the latest and greatest snapshot:

<dependency>
  <groupId>com.googlecode.gmail4j</groupId>
  <artifactId>gmail4j</artifactId>
  <version>0.5-SNAPSHOT</version>
</dependency>

Example code

Get unread messages

GmailClient client = new RssGmailClient();
GmailConnection connection = new HttpGmailConnection(LoginDialog.getInstance().show("Enter Gmail Login"));
client.setConnection(connection);
final List<GmailMessage> messages = client.getUnreadMessages();
for (GmailMessage message : messages) {
    System.out.println(message);
}

Get messages by subject

GmailClient client = new RssGmailClient();
GmailConnection connection = new HttpGmailConnection(LoginDialog.getInstance().show("Enter Gmail Login"));
client.setConnection(connection);
final List<GmailMessage> messages = client.getMessagesBy(GmailClient.EmailSearchStrategy.SUBJECT,
        "Test mail subject. Unicode: ąžuolėlį");
for (GmailMessage message : messages) {
    System.out.println(message);
}

Get messages by sent date greater than

GmailClient client = new RssGmailClient();
GmailConnection connection = new HttpGmailConnection(LoginDialog.getInstance().show("Enter Gmail Login"));
client.setConnection(connection);
final List<GmailMessage> messages = client.getMessagesBy(GmailClient.EmailSearchStrategy.DATE_GT,
        new Date().toString());
for (GmailMessage message : messages) {
    System.out.println(message);
}

Get messages by keyword

GmailClient client = new RssGmailClient();
GmailConnection connection = new HttpGmailConnection(LoginDialog.getInstance().show("Enter Gmail Login"));
client.setConnection(connection);
final List<GmailMessage> messages = client.getMessagesBy(
        GmailClient.EmailSearchStrategy.KEYWORD,"Unicode");
for (GmailMessage message : messages) {
    System.out.println(message);
}

Get unread messages via proxy

GmailClient client = new RssGmailClient();
HttpGmailConnection connection = new HttpGmailConnection(LoginDialog.getInstance().show("Enter Gmail Login"));
connection.setProxy("proxy.example.com", 8080);
connection.setProxyCredentials(LoginDialog.getInstance().show("Enter Proxy Login"));
client.setConnection(connection);
final List<GmailMessage> messages = client.getUnreadMessages();
for (GmailMessage message : messages) {
    System.out.println(message);
}

There are more examples in API docs.

Compiling

To build a jar, run:

mvn clean package

You will be asked for valid Gmail account username/password to run the tests. You can register Gmail account for this purpose or use your own. Registering fresh test account is recommended.

You can also skip tests when building a jar:

mvn clean package -Dmaven.test.skip=true

Maven Site

For reports, API docs and more, please check Gmail4j Maven Site.

Changelog

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].