简体   繁体   English

使用pop3查询有关如何使用javamail从gmail帐户中删除电子邮件

[英]Query regarding how to delete emails from gmail account using javamail using pop3

We are getting correct number of mails which are deleted.But from the gmail account the mails are not deleted. 我们正在获取正确数量的已删除邮件。但是,从gmail帐户中不会删除邮件。 The expunge() method doesn't work for pop3. expunge()方法不适用于pop3。 Is there any similar method for pop3 to permanently delete the emails? pop3是否有任何类似的方法可以永久删除电子邮件? We also set folder.close(true) 我们还设置了folder.close(true)

The code is as follows- 代码如下-

import java.util.Date; 
import java.util.Properties; 

import javax.mail.Address; 
import javax.mail.FetchProfile; 
import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Part; 
import javax.mail.Session; 
import javax.mail.Store; 
import javax.mail.URLName; 
import javax.mail.internet.ContentType; 
import javax.mail.internet.ParseException; 

import com.sun.mail.imap.protocol.FLAGS;
import com.sun.mail.pop3.POP3SSLStore; 

public class GmailUtilities { 

    private Session session = null; 
    private Store store = null; 
    private String username, password; 
    private Folder folder; 

    public GmailUtilities() { 

    } 

    public void setUserPass(String username, String password) { 
        this.username = username; 
        this.password = password; 
    } 

    public void connect() throws Exception { 

        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; 

        Properties pop3Props = new Properties(); 

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); 
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false"); 
        pop3Props.setProperty("mail.pop3.port",  "995"); 
        pop3Props.setProperty("mail.pop3.socketFactory.port", "995"); 

        URLName url = new URLName("pop3", "pop.gmail.com", 995, "", 
                username, password); 

        session = Session.getInstance(pop3Props, null); 
        store = new POP3SSLStore(session, url); 
        store.connect();   //uses session properties

    } 

    public void openFolder(String folderName) throws Exception { 

        // Open the Folder 
        folder = store.getDefaultFolder(); 

        folder = folder.getFolder(folderName); 

        if (folder == null) { 
            throw new Exception("Invalid folder"); 
        } 

        // try to open read/write and if that fails try read-only 
        try { 

            folder.open(Folder.READ_WRITE); 

        } catch (MessagingException ex) { 

            folder.open(Folder.READ_ONLY); 

        } 
    } 

    public void closeFolder() throws Exception { 
        folder.close(false); 
    } 

    public int getMessageCount() throws Exception { 
        return folder.getMessageCount(); 
    } 

 public void printAllMessages() throws Exception { 

        // Attributes & Flags for all messages .. 
        Message[] msgs = folder.getMessages(); 

        // Use a suitable FetchProfile 
        FetchProfile fp = new FetchProfile(); 
        fp.add(FetchProfile.Item.ENVELOPE);         
        folder.fetch(msgs, fp); 

        for (int i = 0; i < msgs.length; i++) { 
            System.out.println("--------------------------"); 
            System.out.println("MESSAGE #" + (i + 1) + ":"); 
            dumpPart(msgs[i]); 
msgs[i].setFlag(FLAGS.Flag.DELETED, true);

System.out.println("Message " +(i+1)+" is deleted");
        } 
folder.close(true);          
    } 


    public static void dumpPart(Part p) throws Exception { 
        if (p instanceof Message) 
            dumpEnvelope((Message)p); 
       Object content = p.getContent(); 
      System.out.println(content); 
        String ct = p.getContentType(); 
        try { 
            pr("CONTENT-TYPE: " + (new ContentType(ct)).toString()); 
        } catch (ParseException pex) { 
            pr("BAD CONTENT-TYPE: " + ct); 
        } 

        /* 
         * Using isMimeType to determine the content type avoids 
         * fetching the actual content data until we need it. 
         */ 
        if (p.isMimeType("text/plain")) { 
            pr("This is plain text"); 
            pr("---------------------------"); 
            System.out.println((String)p.getContent());         
        } else { 

            // just a separator 
            pr("---------------------------"); 

        } 
    } 

    public static void dumpEnvelope(Message m) throws Exception {         
        pr(" "); 
        Address[] a; 
        // FROM 
        if ((a = m.getFrom()) != null) { 
            for (int j = 0; j < a.length; j++) 
                pr("FROM: " + a[j].toString()); 
        } 

        // TO 
        if ((a = m.getRecipients(Message.RecipientType.TO)) != null) { 
            for (int j = 0; j < a.length; j++) { 
                pr("TO: " + a[j].toString());                 
            } 
        } 

        // SUBJECT 
        pr("SUBJECT: " + m.getSubject()); 

        // DATE 
        Date d = m.getSentDate(); 
        pr("SendDate: " + 
                (d != null ? d.toString() : "UNKNOWN")); 


    } 

    static String indentStr = "                                               "; 
    static int level = 0; 

    /** 
     * Print a, possibly indented, string. 
     */
    public static void pr(String s) { 

        System.out.print(indentStr.substring(0, level * 2)); 
        System.out.println(s); 
    } 

    public static void main(String[] args) { 

        try { 

            GmailUtilities gmail = new GmailUtilities(); 
            gmail.setUserPass("abc@gmail.com","password"); 
            gmail.connect(); 
            gmail.openFolder("INBOX"); 

            int totalMessages = gmail.getMessageCount(); 

            System.out.println("Total messages = " + totalMessages); 

            gmail.printAllMessages(); 

        } catch(Exception e) { 
            e.printStackTrace(); 
        } 

    } 

}

Probably not related to your problem, but you might want to read this list of common JavaMail mistakes . 可能与您的问题无关,但是您可能希望阅读此常见JavaMail错误列表。

As for why the messages aren't being deleted, I don't know. 至于为什么不删除邮件,我不知道。 Make sure you're really opening the folder read/write. 确保您确实打开了读/写文件夹。 The protocol trace might provide some clues as to what's really going on. 协议跟踪可能会提供一些有关实际情况的线索。

Gmail doesn't let you delete messages via the POP3 protocol. Gmail不允许您通过POP3协议删除邮件。

Deleting messages in Gmail after POP downloads POP下载后删除Gmail中的邮件

GMail - Behavior Explained GMail-行为解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM