001package edu.pdx.cs410J.grader; 002 003import jakarta.mail.*; 004 005import java.io.PrintStream; 006import java.util.Properties; 007 008/** 009 * This program reads your INBOX and prints out the subjects of each 010 * of the emails. 011 */ 012public class PrintEmailSubjects { 013 private static final PrintStream out = System.out; 014 private static final PrintStream err = System.err; 015 016 /** 017 * Prints usage information about this program 018 */ 019 private static void usage(String s) { 020 err.println("\n** " + s + "\n"); 021 err.println("usage: java PrintEmailSubjects mailbox"); 022 err.println(); 023 System.exit(1); 024 } 025 026 public static void main(String[] args) throws Throwable { 027 String mailbox = null; 028 boolean DEBUG = false; 029 030 for (String arg : args) { 031 if (arg.equals("-debug")) { 032 DEBUG = true; 033 034 } else if (mailbox == null) { 035 mailbox = arg; 036 037 } else { 038 usage("Spurious command line: " + arg); 039 } 040 } 041 042 if (mailbox == null) { 043 usage("Missing mailbox"); 044 } 045 046 // Get a connection to the mail server 047 Properties props = new Properties(); 048 Session session = Session.getDefaultInstance(props, null); 049 session.setDebug(DEBUG); 050 051 Provider[] providers = session.getProviders(); 052 for (Provider provider : providers) { 053 out.println(provider); 054 } 055 056 Store store = session.getStore("imap"); 057 Folder folder = store.getFolder(mailbox); 058 Message[] messages = folder.getMessages(); 059 out.println("Folder " + folder.getName() + " with " + 060 messages.length + " messages"); 061 for (int i = 0; i < messages.length; i++) { 062 Message message = messages[i]; 063 out.println(" " + i + ": " + message.getSubject()); 064 } 065 066 out.println(); 067 068 } 069 070}