如何使用java访问gmail中的标签文件夹?

时间:2014-10-27 21:58:52

标签: java

我们可以从gmail收件箱中读取邮件,但我们可以从标签上读取吗? 如果我从http://harikrishnan83.wordpress.com/2009/01/24/access-gmail-with-imap-using-java-mail-api/

中选择以下示例
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class InboxReader {

    public static void main(String args[]) {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getDefaultInstance(props, null);
            Store store = session.getStore("imaps");
            store.connect("imap.gmail.com", "<username>", "password");
            System.out.println(store);

            Folder inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message messages[] = inbox.getMessages();
            for(Message message:messages) {
                System.out.println(message);
            }
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (MessagingException e) {
            e.printStackTrace();
            System.exit(2);
        }

    }

}

如果我更改&#34;收件箱&#34;通过标签名称,它会抛出错误:找不到收件箱。 有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

尝试以下适用于我的代码:

store.getFolder("FolderNameGoesHere");

private static Store getConnection() throws MessagingException {
    Properties properties;
    Session session;
    Store store;
    properties = new Properties();
    properties.setProperty("mail.host", "imap.gmail.com");
    properties.setProperty("mail.port", "995");
    properties.setProperty("mail.transport.protocol", "imaps");
    session = Session.getInstance(properties,
                                  new javax.mail.Authenticator() {
                                      protected PasswordAuthentication getPasswordAuthentication() {
                                          return new PasswordAuthentication("dummy@gmail.com",
                                                                            "dummy");
                                      }
                                  });
    store = session.getStore("imaps");
    store.connect();
    return store;

}

public static boolean isMailReceivedBySubject(String subject,String folder) throws MessagingException {
    Store store = null;
    boolean emailReceived = false;
    try {
        store = getConnection();
        Folder mailFolder = store.getFolder(folder);
        mailFolder.open(Folder.READ_WRITE);
        SearchTerm st = new AndTerm(new SubjectTerm(subject), new BodyTerm(subject));
        Message[] messages = mailFolder.search(st);
        for (Message message : messages) {
            System.out.println("message  : " + message.getSubject());
            if (message.getSubject().contains(subject)) {
                System.out.println("Found the email subject : " + subject);
                emailReceived = true;
                break;
            }
        }
        return emailReceived;
    }finally {
        if (store != null) {
            store.close();
        }
    }
}