| 1 |
/* |
| 2 |
* Copyright (c) 1996, 2020 Oracle and/or its affiliates. All rights reserved. |
| 3 |
* |
| 4 |
* This program and the accompanying materials are made available under the |
| 5 |
* terms of the Eclipse Distribution License v. 1.0, which is available at |
| 6 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 7 |
* |
| 8 |
* SPDX-License-Identifier: BSD-3-Clause |
| 9 |
*/ |
| 10 |
|
| 11 |
import java.util.Properties; |
| 12 |
import jakarta.mail.*; |
| 13 |
|
| 14 |
import com.sun.mail.imap.*; |
| 15 |
|
| 16 |
/** |
| 17 |
* Demo app that exercises the Message interfaces. |
| 18 |
* List information about folders. |
| 19 |
* |
| 20 |
* @author John Mani |
| 21 |
* @author Bill Shannon |
| 22 |
*/ |
| 23 |
|
| 24 |
public class folderlist { |
| 25 |
static String protocol = null; |
| 26 |
static String host = null; |
| 27 |
static String user = null; |
| 28 |
static String password = null; |
| 29 |
static String url = null; |
| 30 |
static String root = null; |
| 31 |
static String pattern = "%"; |
| 32 |
static boolean recursive = false; |
| 33 |
static boolean verbose = false; |
| 34 |
static boolean debug = false; |
| 35 |
|
| 36 |
public static void main(String argv[]) throws Exception { |
| 37 |
int optind; |
| 38 |
for (optind = 0; optind < argv.length; optind++) { |
| 39 |
if (argv[optind].equals("-T")) { |
| 40 |
protocol = argv[++optind]; |
| 41 |
} else if (argv[optind].equals("-H")) { |
| 42 |
host = argv[++optind]; |
| 43 |
} else if (argv[optind].equals("-U")) { |
| 44 |
user = argv[++optind]; |
| 45 |
} else if (argv[optind].equals("-P")) { |
| 46 |
password = argv[++optind]; |
| 47 |
} else if (argv[optind].equals("-L")) { |
| 48 |
url = argv[++optind]; |
| 49 |
} else if (argv[optind].equals("-R")) { |
| 50 |
root = argv[++optind]; |
| 51 |
} else if (argv[optind].equals("-r")) { |
| 52 |
recursive = true; |
| 53 |
} else if (argv[optind].equals("-v")) { |
| 54 |
verbose = true; |
| 55 |
} else if (argv[optind].equals("-D")) { |
| 56 |
debug = true; |
| 57 |
} else if (argv[optind].equals("--")) { |
| 58 |
optind++; |
| 59 |
break; |
| 60 |
} else if (argv[optind].startsWith("-")) { |
| 61 |
System.out.println( |
| 62 |
"Usage: folderlist [-T protocol] [-H host] [-U user] [-P password] [-L url]"); |
| 63 |
System.out.println( |
| 64 |
"\t[-R root] [-r] [-v] [-D] [pattern]"); |
| 65 |
System.exit(1); |
| 66 |
} else { |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
if (optind < argv.length) |
| 71 |
pattern = argv[optind]; |
| 72 |
|
| 73 |
// Get a Properties object |
| 74 |
Properties props = System.getProperties(); |
| 75 |
|
| 76 |
// Get a Session object |
| 77 |
Session session = Session.getInstance(props, null); |
| 78 |
session.setDebug(debug); |
| 79 |
|
| 80 |
// Get a Store object |
| 81 |
Store store = null; |
| 82 |
Folder rf = null; |
| 83 |
if (url != null) { |
| 84 |
URLName urln = new URLName(url); |
| 85 |
store = session.getStore(urln); |
| 86 |
store.connect(); |
| 87 |
} else { |
| 88 |
if (protocol != null) |
| 89 |
store = session.getStore(protocol); |
| 90 |
else |
| 91 |
store = session.getStore(); |
| 92 |
|
| 93 |
// Connect |
| 94 |
if (host != null || user != null || password != null) |
| 95 |
store.connect(host, user, password); |
| 96 |
else |
| 97 |
store.connect(); |
| 98 |
} |
| 99 |
|
| 100 |
// List namespace |
| 101 |
if (root != null) |
| 102 |
rf = store.getFolder(root); |
| 103 |
else |
| 104 |
rf = store.getDefaultFolder(); |
| 105 |
|
| 106 |
dumpFolder(rf, false, ""); |
| 107 |
if ((rf.getType() & Folder.HOLDS_FOLDERS) != 0) { |
| 108 |
Folder[] f = rf.list(pattern); |
| 109 |
for (int i = 0; i < f.length; i++) |
| 110 |
dumpFolder(f[i], recursive, " "); |
| 111 |
} |
| 112 |
|
| 113 |
store.close(); |
| 114 |
} |
| 115 |
|
| 116 |
static void dumpFolder(Folder folder, boolean recurse, String tab) |
| 117 |
throws Exception { |
| 118 |
System.out.println(tab + "Name: " + folder.getName()); |
| 119 |
System.out.println(tab + "Full Name: " + folder.getFullName()); |
| 120 |
System.out.println(tab + "URL: " + folder.getURLName()); |
| 121 |
|
| 122 |
if (verbose) { |
| 123 |
if (!folder.isSubscribed()) |
| 124 |
System.out.println(tab + "Not Subscribed"); |
| 125 |
|
| 126 |
if ((folder.getType() & Folder.HOLDS_MESSAGES) != 0) { |
| 127 |
if (folder.hasNewMessages()) |
| 128 |
System.out.println(tab + "Has New Messages"); |
| 129 |
System.out.println(tab + "Total Messages: " + |
| 130 |
folder.getMessageCount()); |
| 131 |
System.out.println(tab + "New Messages: " + |
| 132 |
folder.getNewMessageCount()); |
| 133 |
System.out.println(tab + "Unread Messages: " + |
| 134 |
folder.getUnreadMessageCount()); |
| 135 |
} |
| 136 |
if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) |
| 137 |
System.out.println(tab + "Is Directory"); |
| 138 |
|
| 139 |
/* |
| 140 |
* Demonstrate use of IMAP folder attributes |
| 141 |
* returned by the IMAP LIST response. |
| 142 |
*/ |
| 143 |
if (folder instanceof IMAPFolder) { |
| 144 |
IMAPFolder f = (IMAPFolder)folder; |
| 145 |
String[] attrs = f.getAttributes(); |
| 146 |
if (attrs != null && attrs.length > 0) { |
| 147 |
System.out.println(tab + "IMAP Attributes:"); |
| 148 |
for (int i = 0; i < attrs.length; i++) |
| 149 |
System.out.println(tab + " " + attrs[i]); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
System.out.println(); |
| 155 |
|
| 156 |
if ((folder.getType() & Folder.HOLDS_FOLDERS) != 0) { |
| 157 |
if (recurse) { |
| 158 |
Folder[] f = folder.list(); |
| 159 |
for (int i = 0; i < f.length; i++) |
| 160 |
dumpFolder(f[i], recurse, tab + " "); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |