我们如何使用Java在gmail中打开具有特定主题和特定日期的邮件?

时间:2018-03-29 09:41:42

标签: java pop3

我正在尝试在特定日期使用特定主题在Gmail中打开邮件。 我已经写下了从Inbox文件夹中的Gmail中获取所有邮件的代码。 我们如何将特定的邮件内容转换为字符串格式。而不是搜索所有邮件。 我们如何搜索今天日期的邮件主题名称?

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;

public class EmailRead {

    private static final String MAIL_POP_HOST = "pop.gmail.com";
    private static final String MAIL_STORE_TYPE = "pop3";
    private static final String POP_USER = "users email";
    private static final String POP_PASSWORD = "users password";
    private static final String POP_PORT = "995";

    public static void getMails(String user, String password) {
        user = "slautomation2018@gmail.com";
        password = "dontKnow";
        try {
            // create properties field
            Properties properties = new Properties();
            properties.put("mail.pop3.host", MAIL_POP_HOST);
            properties.put("mail.pop3.port", POP_PORT);
            properties.put("mail.pop3.starttls.enable", "true");
            properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            // Session emailSession = Session.getDefaultInstance(properties);
            Session emailSession = Session.getInstance(properties, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(POP_USER, POP_PASSWORD);
                }
            });
            // create the POP3 store object and connect with the pop server
            Store store = emailSession.getStore(MAIL_STORE_TYPE);
            store.connect(MAIL_POP_HOST, user, password);
            // create the folder object and open it
            Folder emailFolder = store.getFolder("INBOX");
            emailFolder.open(Folder.READ_ONLY);

            // retrieve the messages from the folder in an array and print it

            Message[] messages = emailFolder.getMessages();
            int m = messages.length-1;
            int y = messages.length-2;
            System.out.println(messages[m]);
            System.out.println(messages[y]);
            System.out.println("messages.length---" + messages.length);
            for (int i = 0, n = messages.length; i < n; i++) {
                Message message = messages[i];
                System.out.println("---------------------------------");
                System.out.println("Email Number " + (i + 1));
                System.out.println(message.getSubject());
                System.out.println(message.getFrom()[0]);
                System.out.println(message.getContent().toString());
                System.out.println(message.getReceivedDate());

            }



            // close the store and folder objects
            emailFolder.close(false);
            store.close();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        getMails(POP_USER, POP_PASSWORD);
    }
}

0 个答案:

没有答案