当前位置: 首页 > 工具软件 > Gmail4J > 使用案例 >

gmail4j使用实例

郝君博
2023-12-01
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);
        }


    }


}

 类似资料: