/*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Demo app that exercises the Message interfaces.
* Show information about and contents of messages.
*
* @author John Mani
* @author Bill Shannon
*/
public class msgshow {
static boolean verbose = false;
static boolean debug = false;
static boolean showStructure = false;
static boolean showMessage = false;
static boolean showAlert = false;
static boolean saveAttachments = false;
int optind;
verbose = true;
debug = true;
showStructure = true;
saveAttachments = true;
showMessage = true;
showAlert = true;
optind++;
break;
"Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
"\t[-P password] [-f mailbox] [msgnum ...] [-v] [-D] [-s] [-S] [-a]");
"or msgshow -m [-v] [-D] [-s] [-S] [-f msg-file]");
} else {
break;
}
}
try {
// Get a Properties object
// Get a Session object
if (showMessage) {
else
}
// Get a Store object
if (showAlert) {
public void notification(StoreEvent e) {
String s;
s = "ALERT: ";
else
s = "NOTICE: ";
}
});
}
} else {
else
// Connect
else
}
// Open the Folder
}
mbox = "INBOX";
}
try {
} catch (MessagingException ex) {
}
if (totalMessages == 0) {
}
if (verbose) {
}
// Attributes & Flags for all messages ..
// Use a suitable FetchProfile
dumpEnvelope(msgs[i]);
// dumpPart(msgs[i]);
}
} else {
try {
dumpPart(m);
} catch (IndexOutOfBoundsException iex) {
}
}
}
}
}
if (p instanceof Message)
dumpEnvelope((Message)p);
/** Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(is);
int c;
while ((c = is.read()) != -1)
System.out.write(c);
**/
try {
} catch (ParseException pex) {
}
/*
* Using isMimeType to determine the content type avoids
* fetching the actual content data until we need it.
*/
if (p.isMimeType("text/plain")) {
pr("This is plain text");
pr("---------------------------");
if (!showStructure && !saveAttachments)
} else if (p.isMimeType("multipart/*")) {
pr("This is a Multipart");
pr("---------------------------");
level++;
for (int i = 0; i < count; i++)
level--;
} else if (p.isMimeType("message/rfc822")) {
pr("This is a Nested Message");
pr("---------------------------");
level++;
level--;
} else {
if (!showStructure && !saveAttachments) {
/*
* If we actually want to see the data, and it's not a
* MIME type we know, fetch it and check its Java type.
*/
Object o = p.getContent();
if (o instanceof String) {
pr("This is a string");
pr("---------------------------");
} else if (o instanceof InputStream) {
pr("This is just an input stream");
pr("---------------------------");
int c;
} else {
pr("This is an unknown type");
pr("---------------------------");
}
} else {
// just a separator
pr("---------------------------");
}
}
/*
* If we're saving attachments, write out anything that
* looks like an attachment into an appropriately named
* file. Don't overwrite existing files to prevent
* mistakes.
*/
!p.isMimeType("multipart/*")) {
// many mailers don't include a Content-Disposition
try {
if (f.exists())
// XXX - could try a series of names
throw new IOException("file exists");
((MimeBodyPart)p).saveFile(f);
} catch (IOException ex) {
}
pr("---------------------------");
}
}
}
pr("This is the message envelope");
pr("---------------------------");
Address[] a;
// FROM
for (int j = 0; j < a.length; j++)
}
// REPLY TO
if ((a = m.getReplyTo()) != null) {
for (int j = 0; j < a.length; j++)
}
// TO
for (int j = 0; j < a.length; j++) {
}
}
}
// SUBJECT
// DATE
Date d = m.getSentDate();
pr("SendDate: " +
// FLAGS
boolean first = true;
String s;
s = "\\Answered";
s = "\\Deleted";
s = "\\Draft";
s = "\\Flagged";
s = "\\Recent";
s = "\\Seen";
else
continue; // skip it
if (first)
first = false;
else
}
if (first)
first = false;
else
}
// X-MAILER
else
pr("X-Mailer NOT available");
}
/**
* Print a, possibly indented, string.
*/
if (showStructure)
}
}