java将邮件保存到本地文件夹_JavaMail 邮件文件夹管理

这篇博客介绍了如何使用JavaMail API管理邮件文件夹,包括打开、创建、删除和重命名文件夹,以及文件夹中邮件的管理,如复制、删除和搜索邮件。此外,还涵盖了设置和获取邮件的标志以及文件夹中邮件数量的相关方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

到目前为止,我们已经在前面的章节主要介绍收件箱文件夹的工作。这是大多数邮件位于其中的默认文件夹。有些系统可能会调用它的收件箱和其他一些可能被其他一些名字来称呼它。但是,你总是可以从JavaMail API为使用该名称的收件箱访问它。

JavaMail API为代表文件夹的文件夹抽象类的实例:

publicabstractclassFolderextendsObject

这个类声明请求命名文件夹从服务器,从文件夹中删除邮件,搜索文件夹中特定消息,列出文件夹中的邮件,等等方法。

打开文件夹

我们不能直接创建一个文件夹中 Folder 类唯一的构造函数是受保护的。我们可以得到一个文件夹从:

Session

Store

另外的Folder

上述所有的类都有一个类似的getFolder()方法类似签名:

publicabstractFoldergetFolder(Stringname)throwsMessagingException

一些在其中获取Folder 对象帮助的方法有:

方法

描述

boolean exists()

Checks if the folder really exists. Use this method before getting the Folder object.

abstract void open(int mode)

When you get a Folder, its closed. Use this method to open it. modecan be Folder.READ_ONLY or Folder.READ_WRITE.

abstract boolean isOpen()

This method returns true if the folder is open, false if it’s closed

abstract void close(boolean expunge)

Closes the folder. If the expunge argument is true, any deleted messages in the folder are deleted from the actual file on the server. Otherwise, they’re simply marked asdeleted, but the messages can still be undeleted.

基本文件夹信息

以下是一些它返回有关一个文件夹的基本信息,文件夹类中的方法:

方法

描述

abstract String getName()

Returns the name of the folder, such as "TutorialsPoint Mail"

abstract String getFullName()

Returns the complete hierarchical name from the root such as “books/Manisha/TutorialsPoint Mail”.

URLName getURLName()

Return a URLName representing this folder.

abstract Folder getParent()

Returns the name of the folder that contains this folder i.e the parent folder. E.g "Manisha" from the previous "TutorialsPoint Mail" example.

abstract int getType()

Returns an int indicating whether the folder can contain messages and/or other folders.

int getMode()

It returns one of the two named constants Folder.READ_ONLY or Folder.READ_WRITE or -1 when the mode is unknown.

Store getStore()

Returns the Store object from which this folder was retrieved.

abstract char getSeparator()

Return the delimiter character that separates this Folder's pathname from the names of immediate subfolders.

管理文件夹

以下是一些有助于管理文件夹的方法:

方法

描述

abstract boolean create(int type)

This creates a new folder in this folder’s Store. Where typewould be:Folder.HOLDS_MESSAGES or Folder.HOLDS_FOLDERS. Returns true if folder is successfully created else returns false.

abstract boolean delete(boolean recurse)

This deletes the folder only if the folder is closed. Otherwise, it throws an IllegalStateException. If recurse istrue, then subfolders are deleted.

abstract boolean renameTo(Folder f)

This changes the name of this folder. A folder must be closed to be renamed. Otherwise, an IllegalStateException is thrown.

在文件夹管理邮件

以下是一些帮助文件夹管理邮件的方法:

方法

描述

abstract voidappendMessages(Message[] messages)

As the name implies, the messages in the array are placed at the end of this folder.

void copyMessages(Message[] messages, Folder destination)

This copies messages from this folder into a specified folder given as an argument.

abstract Message[] expunge()

To delete a message from a folder, set its Flags.Flag.DELETED flag to true. To physically remove deleted messages from a folder, you have to call this method.

列出文件夹的内容

有四种方法可以列出一个文件夹中包含的文件夹:

方法

描述

Folder[] list()

This returns an array listing the folders that this folder contains.

Folder[] listSubscribed()

This returns an array listing all the subscribed folders that this folder contains.

abstract Folder[] list(String pattern)

This is similar to the list() method except that it allows you to specify a pattern. The pattern is a string giving the name of the folders that match.

Folder[] listSubscribed(String pattern)

This is similar to the listSubscribed() method except that it allows you to specify a pattern. The pattern is a string giving the name of the folders that match.

检查邮件

方法

描述

abstract int getMessageCount()

This method can be invoked on an open or closed folder. However, in the case of a closed folder, this method may (or may not) return -1 to indicate that the exact number of messages isn’t easily available.

abstract boolean hasNewMessages()

This returns true if new messages have been added to the folder since it was last opened.

int getNewMessageCount()

It returns the new message count by checking messages in the folder whose RECENT flag is set.

int getUnreadMessageCount()

This can be invoked on either an open or a closed folder. However, in the case of a closed folder, it may return -1 to indicate that the real answer would be too expensive to obtain.

获取信息的文件夹

Folder类提供了四种方法,用于检索从打开文件夹的邮件:

方法

描述

abstract Message getMessage(int messageNumber)

This returns the nth message in the folder. The first message in the folder is number 1.

Message[] getMessages()

This returns an array of Message objects representing all the messages in this folder.

Message[] getMessages(int start, int end)

This returns an array of Message objects from the folder, beginning with start and finishing with end, inclusive.

Message[] getMessages(int[] messageNumbers)

This returns an array containing only those messages specifically identified by number in the messageNumbersarray.

void fetch(Message[] messages, FetchProfile fp)

Prefetch the items specified in the FetchProfile for the given Messages. The FetchProfile argument specifies which headers in the messages to prefetch.

搜索文件夹

如果服务器支持搜索(许多IMAP服务器做最POP服务器没有),很容易搜索的文件夹,以满足某些条件的邮件。标准编码在搜索关键词的对象。以下是两种搜索方法:

方法

描述

Message[] search(SearchTerm term)

Search this Folder for messages matching the specified search criterion. Returns an array containing the matching messages. Returns an empty array if no matches were found.

Message[] search(SearchTerm term, Message[] messages)

Search the given array of messages for those that match the specified search criterion. Returns an array containing the matching messages. Returns an empty array if no matches were found. The the specified Message objects must belong to this folder.

Flags

当你需要改变标志的文件夹对整个消息集标志的修改是很有用的。以下是在文件夹类提供的方法:

方法

描述

void setFlags(Message[] messages, Flags flag, boolean value)

Sets the specified flags on the messages specified in the array.

void setFlags(int start, int end, Flags flag, boolean value)

Sets the specified flags on the messages numbered from start through end, both start and end inclusive.

void setFlags(int[] messageNumbers, Flags flag, boolean value)

Sets the specified flags on the messages whose message numbers are in the array.

abstract Flags getPermanentFlags()

Returns the flags that this folder supports for all messages.

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值