Java教程

java获取某一天的邮件

本文主要是介绍java获取某一天的邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取某一天的邮件:
` public static void main(String[] args) throws Exception {

    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.store.protocol", "imap");
    props.put("mail.smtp.class", "com.sum.mail.smtp.SMTPTransport");
    props.put("mail.imap.class", "com.sum.mail.imap.IMAPStore");
    props.put("mail.smtp.host", "pop.163.com");

    Session session = Session.getInstance(props);
    Store store = session.getStore("pop3");
    store.connect("pop.163.com", "xxx", "xxx");

    Folder folder = store.getFolder("inbox");
    folder.open(Folder.READ_ONLY);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = simpleDateFormat.parse("2021-10-01");
    Date date2 = simpleDateFormat.parse("2021-10-02");
    SearchTerm comparisonTermGe = new SentDateTerm(DateTerm.GE, date);
    SearchTerm comparisonTermLt = new SentDateTerm(DateTerm.LT, date2);
    SearchTerm comparisonAndTerm = new AndTerm(comparisonTermGe, comparisonTermLt);
    Message[] messages = folder.search(comparisonAndTerm);

    System.out.println("你一共有" + messages.length + "封邮件");
    System.out.println("==============================================");
    for (int i = 0; i < messages.length; i++) {
        System.out.println("==============================================");

        Message message = messages[i];
        System.out.println("邮件主题" + message.getSubject());
        System.out.println("发送日期" + message.getSentDate());
        System.out.println("邮件内容" + message.getContentType());
        //System.out.println("邮件内容2" + message.getContent());
    }
    folder.close(false);
    store.close();

}`
这篇关于java获取某一天的邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!