从邮件服务器读取已发送的邮件

时间:2010-10-11 09:21:53

标签: java

我知道如何从INBOX文件夹中检索邮件...但现在我想从SENT ITEMS文件夹中检索邮件...我正在使用imap来检索数据... 让我知道我应该在这个函数中传递什么参数来从SENT ITEMS文件夹中获取邮件 Folder folder=store.getFolder("inbox");我应该更改收件箱,因为我想知道那个字符串......

4 个答案:

答案 0 :(得分:3)

这里没有标准名称。 IMAP spec要求将收件箱称为“INBOX”,但不会明确定义其他文件夹。毕竟它只是一个文件夹的名称 - 一些提供商将使用“已发送”,有些将使用“已发送的项目”,您甚至可能会看到其他一些变体。

我建议列出服务器知道的文件夹,然后从那里选择合适的文件夹(交互式,或者如果运行无头,可能会在名称中使用“发送”)。总体上更好的选择可能是使其成为可配置参数(如果您的应用程序已经有属性文件)。

当然,如果这是一次性项目,您可以硬编码特定服务器的值。但如果你想要做得恰当,你需要灵活。

答案 1 :(得分:2)

我为我的问题找到了解决方案.... 我使用此代码列出邮件服务器中的文件夹 并在getFolder()函数中传递这些值...它工作正常..

Folder[] folderList = store.getDefaultFolder().list();
        for (int i = 0; i < folderList.length; i++) {
            System.out.println(folderList[i].getFullName());
        }

答案 2 :(得分:1)

Gmail将邮件发送到Error:(28,1) Expected'end'(to close 'else' at line 11),got <eof> 文件夹中名为Sent Mail的文件夹下。所以我能够通过这个获得发送邮件文件夹;

[Gmail]

答案 3 :(得分:0)

此代码将检索所有邮件,并将打印内容并存储在本地(如果有任何附件)

public class MailReader {
        Folder inbox;
        public MailReader() {
            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");
                /* Mention the folder name which you want to read. */
                inbox = store.getFolder("Inbox");
                System.out.println("No of Unread Messages : "
                        + inbox.getMessageCount() + " "
                        + inbox.getUnreadMessageCount());

                /* Open the inbox using store. */
                inbox.open(Folder.READ_ONLY);

                /*
                 * Get the messages which is unread in the Inbox Message messages[]
                 * = inbox.search(new FlagTerm( new Flags(Flag.SEEN), false));
                 */
                Message messages[] = inbox.getMessages();
                /* Use a suitable FetchProfile */
                FetchProfile fp = new FetchProfile();
                fp.add(FetchProfile.Item.ENVELOPE);
                fp.add(FetchProfile.Item.CONTENT_INFO);
                inbox.fetch(messages, fp);

                try {
                    printAllMessages(messages);
                    inbox.close(true);
                    store.close();
                } catch (Exception ex) {
                    System.out.println("Exception arise at the time of read mail");
                    ex.printStackTrace();
                }
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
                System.exit(1);
            } catch (MessagingException e) {
                e.printStackTrace();
                System.exit(2);
            }
        }

        public void printAllMessages(Message[] msgs) throws Exception {
            for (int i = 0; i < msgs.length; i++) {
                System.out.println("MESSAGE #" + (i + 1) + ":");
                printEnvelope(msgs[i]);
            }
        }

        /* Print the envelope(FromAddress,ReceivedDate,Subject) */
        public void printEnvelope(Message message) throws Exception {
            Address[] a;
            // FROM
            if ((a = message.getFrom()) != null) {
                for (int j = 0; j < a.length; j++) {
                    System.out.println("FROM: " + a[j].toString());
                }
            }
            // TO
            if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
                for (int j = 0; j < a.length; j++) {
                    System.out.println("TO: " + a[j].toString());
                }
            }
            String subject = message.getSubject();
            Date receivedDate = message.getReceivedDate();
            String content = message.getContent().toString();
            System.out.println("Subject : " + subject);
            System.out.println("Received Date : " + receivedDate.toString());
            System.out.println("Content : " + content);
            getContent(message);
        }

        public void getContent(Message msg) {
            try {
                String contentType = msg.getContentType();
                System.out.println("Content Type : " + contentType);
                Multipart mp = (Multipart) msg.getContent();
                int count = mp.getCount();
                for (int i = 0; i < count; i++) {
                    dumpPart(mp.getBodyPart(i));
                }
            } catch (Exception ex) {
                System.out.println("Exception arise at get Content");
                ex.printStackTrace();
            }
        }

        public void dumpPart(Part p) throws Exception {
            // Dump input stream ..
            if (p.getFileName() == null) {
                return;
            }
            System.out.println("filename:" + p.getFileName());
            System.out.println(p.ATTACHMENT);
            InputStream is = p.getInputStream();
            File file = new File(p.getFileName());
            FileOutputStream fout = null;
            fout = new FileOutputStream(p.getFileName());
            // If "is" is not already buffered, wrap a BufferedInputStream
            // around it.
            if (!(is instanceof BufferedInputStream)) {
                is = new BufferedInputStream(is);
            }
            int c;
            System.out.println("Message : ");
            while ((c = is.read()) != -1) {
                fout.write(c);
            }
            if (fout != null) {
                fout.close();
            }
        }

        public static void main(String args[]) {
            new MailReader();
        }
    }
相关问题