JavaMail API by Elliotte Rusty Harold
Author:Elliotte Rusty Harold [Elliotte Rusty Harold]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449367206
Publisher: O'Reilly Media
Published: 2013-07-22T16:00:00+00:00
Figure 7-1. Pine shows flags as letters in the lefthand column
The getFlags() method returns the flags of a particular message:
public abstract Flags getFlags() throws MessagingException
The isSet() method tests whether a specified flag is set for the given message:
public boolean isSet(Flags.Flag flag) throws MessagingException
Finally, the setFlags() and setFlag() methods set or unset (depending on the second argument) the flag indicated by the first argument:
public abstract void setFlags(Flags flag, boolean set) throws MessagingException, IllegalWriteException, IllegalStateException public void setFlag(Flags.Flag flag, boolean set) throws MessagingException, IllegalWriteException, IllegalStateException
You delete messages by setting their Flags.Flag.DELETED flag to true. For example, to delete message:
message.setFlag(Flags.Flag.DELETED, true);
This only marks the message as deleted. It does not actually expunge it from the file on the server. Until the message is expunged, it can still be undeleted by setting Flags.Flag.DELETED back to false.
Example 7-2 is a slight modification of Example 7-1, HeaderClient, which prints the flags as well. As a general rule, POP servers wonât report flags. Only a protocol that stores messages and forwards them, such as IMAP or mbox, will report flags.
Example 7-2. A program to read mailbox flags
import javax.mail.*; import javax.mail.internet.*; import java.util.*; public class FlagsClient { public static void main(String[] args) { if (args.length == 0) { System.err.println( "Usage: java FlagsClient protocol://username@host/foldername"); return; } URLName server = new URLName(args[0]); try { Session session = Session.getInstance(new Properties(), new MailAuthenticator(server.getUsername())); // Connect to the server and open the folder Folder folder = session.getFolder(server); if (folder == null) { System.out.println("Folder " + server.getFile() + " not found."); System.exit(1); } folder.open(Folder.READ_ONLY); // Get the messages from the server Message[] messages = folder.getMessages(); for (int i = 0; i < messages.length; i++) { System.out.println("------------ Message " + (i+1) + " ------------"); // Get the headers String from = InternetAddress.toString(messages[i].getFrom()); if (from != null) System.out.println("From: " + from); String replyTo = InternetAddress.toString( messages[i].getReplyTo()); if (replyTo != null) System.out.println("Reply-to: " + replyTo); String to = InternetAddress.toString( messages[i].getRecipients(Message.RecipientType.TO)); if (to != null) System.out.println("To: " + to); String cc = InternetAddress.toString( messages[i].getRecipients(Message.RecipientType.CC)); if (cc != null) System.out.println("Cc: " + cc); String bcc = InternetAddress.toString( messages[i].getRecipients(Message.RecipientType.BCC)); if (bcc != null) System.out.println("Bcc: " + bcc); String subject = messages[i].getSubject(); if (subject != null) System.out.println("Subject: " + subject); Date sent = messages[i].getSentDate(); if (sent != null) System.out.println("Sent: " + sent); Date received = messages[i].getReceivedDate(); if (received != null) System.out.println("Received: " + received); // Now test the flags: if (messages[i].isSet(Flags.Flag.DELETED)) { System.out.println("Deleted"); } if (messages[i].isSet(Flags.Flag.ANSWERED)) { System.out.println("Answered"); } if (messages[i].isSet(Flags.Flag.DRAFT)) { System.out.println("Draft"); } if (messages[i].isSet(Flags.Flag.FLAGGED)) { System.out.println("Marked"); } if (messages[i].isSet(Flags.Flag.RECENT)) { System.out.println("Recent"); } if (messages[i].isSet(Flags.Flag.SEEN)) { System.out.println("Read"); } if (messages[i].isSet(Flags.Flag.USER)) { // We don't know what the user flags might be in advance // so they're returned as an array of strings String[] userFlags = messages[i].getFlags().getUserFlags(); for (String flag : userFlags) { System.out.println("User flag: " + flag); } } System.out.println(); } // Close the connection // but don't remove the messages from the server folder.close(false); } catch (MessagingException ex) { ex.printStackTrace(); } // Since we may have brought up a GUI to authenticate, // we can't rely on returning from main() to exit System.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9913)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6819)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6547)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6416)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Functional Programming in JavaScript by Mantyla Dan(4037)
Solidity Programming Essentials by Ritesh Modi(3992)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3783)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3723)
The Ultimate iOS Interview Playbook by Avi Tsadok(3699)
