import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.googlecode.gmail4j.GmailClient;
import com.googlecode.gmail4j.GmailMessage;
import com.googlecode.gmail4j.auth.Credentials;
import com.googlecode.gmail4j.javamail.ImapGmailClient;
import com.googlecode.gmail4j.javamail.ImapGmailConnection;
import com.googlecode.gmail4j.javamail.ImapGmailLabel;
//https://github.com/spajus/gmail4j
//gmail4j使用实例
public class Main {
public static void main(String[] args) throws Exception {
ImapGmailClient client;
ImapGmailConnection connection;
List<GmailMessage> messages;
client = new ImapGmailClient(ImapGmailLabel.INBOX);
Credentials creds;
creds = new Credentials("xxxx@gmail.com", "xxxx".toCharArray());
connection = new ImapGmailConnection();
connection.setLoginCredentials(creds);
client.setConnection(connection);
System.out.println("=====getUnreadMessages====");
messages = client.getUnreadMessages();
for (GmailMessage message : messages) {
System.out.println(message);
}
System.out.println("====KEYWORD==");
messages = client.getMessagesBy(GmailClient.EmailSearchStrategy.KEYWORD,"test");
for (GmailMessage message : messages) {
System.out.println(message);
}
System.out.println("========DATE_GT======");
messages=client.getMessagesBy(GmailClient.EmailSearchStrategy.DATE_GT, new Date().toString());
for (GmailMessage message : messages) {
System.out.println(message);
}
System.out.println("===SUBJECT==");
messages=client.getMessagesBy(GmailClient.EmailSearchStrategy.SUBJECT,
"java ");
for (GmailMessage message : messages) {
System.out.println(message);
}
}
}
gmail4j使用实例
最新推荐文章于 2024-03-22 15:51:42 发布