JavaMail 郵件檔案夾管理


到目前為止,我們已經在前面的章節主要介紹收件箱檔案夾的工作。這是大多數郵件位於其中的預設檔案夾。有些系統可能會呼叫它的收件箱和其他一些可能被其他一些名字來稱呼它。但是,你總是可以從JavaMail API為使用該名稱的收件箱存取它。

JavaMail API為代表檔案夾的檔案夾抽象類的範例:

public abstract class Folder extends Object

這個類宣告請求命名檔案夾從伺服器,從檔案夾中刪除郵件,搜尋檔案夾中特定訊息,列出檔案夾中的郵件,等等方法。

開啟檔案夾

我們不能直接建立一個檔案夾中 Folder 類唯一的建構函式是受保護的。我們可以得到一個檔案夾從:

  •  Session

  • Store

  • 另外的Folder

上述所有的類都有一個類似的getFolder()方法類似簽名:

public abstract Folder getFolder(String name) throws MessagingException

一些在其中獲取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. mode can 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.