JavaMail删除邮件

最近有个邮件的任务,其中有个功能是要删除邮件服务器的邮件

在网上找了几种方式都有点问题,一直是报:POP3 messages are read-only

 

 

修改了下代码就可以删除了,本次只做了下QQ邮箱的测试

        Properties props = new Properties();
        // props.setProperty("mail.smtp.host", "smtp.sina.com");
        props.setProperty("mail.smtp.auth", "true");
       //props.setProperty("mail.transport.protocol", "smtp");
        Session session = Session.getDefaultInstance(props,null);
        URLName urlname = new URLName("pop3","pop.qq.com",110,null,"xxxxx","xxxxx");
       //URLName urlname = new URLName("pop3","pop.exmail.qq.com",110,null,"xxxxx","xxxxx");
        Store store = session.getStore(urlname);
        store.connect();
        Folder folder = store.getFolder("INBOX");
       // folder.open(Folder.READ_ONLY);
        folder.open(Folder.READ_WRITE);
        Message msgs[] = folder.getMessages();
        int count = msgs.length;
        System.out.println("Message Count:"+count);
         msgs[count-1].setFlag(Flags.Flag.DELETED, true);  
        //msgs[count-1].saveChanges();
        //folder.expunge();
        folder.close(true);
        store.close();

 

 

你可能感兴趣的:(deleteEmail,javaMai,l删除邮件,removeEmail)