Home arrow static arrow Java Programming [Archive] - Mail inbox code
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Mail inbox code
This topic has 3 replies on 1 page.

Posts:474
Registered: 3/12/04
Mail inbox code  
Jul 21, 2004 8:28 AM



 
Hi ,this code from javamail-1-3-1 has been converted to jsp.Two compilation errors that i get are
<error>
C:\j2sdkee1.3.1\repository\home-rvn4sxfi5y\web\BookShopContext\Inbox$jsp.java:244: Class or interface declaration expected.
static {

C:\j2sdkee1.3.1\repository\home-rvn4sxfi5y\web\BookShopContext\Inbox$jsp.java:15: class org.apache.jsp.Inbox$jsp must be declared abstract. It does not define void _jspService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) from class
org.apache.jasper.runtime.HttpJspBase.
public class Inbox$jsp extends HttpJspBase {
</error>

and this is the code
<code>

<%@ page language ="java" import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.event.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>

<%!
static String indentStr = " ";
static int level = 0;
static String url=null;
static String protocol="pop3";
static String host = "mail.ineonet.net";
static String user = "aditya_per";
static String password = "7hNWsZ7e";
static String mbox = "INBOX";
static int port = -1;
static boolean verbose = true;
static boolean debug = false;
static boolean showStructure = true;
static boolean showMessage = true;
static boolean showAlert = true;
static boolean saveAttachments = true;
static int attnum = 1;
static int msgnum = -1;

%>
<%

InputStream msgStream = System.in;
// Get a Properties object
Properties props = System.getProperties();

// Get a Session object
Session sess = Session.getInstance(props, null);
sess.setDebug(debug);

if (showMessage) {
MimeMessage msg = new MimeMessage(sess, msgStream);
dumpPart(msg);
System.exit(0);
}

// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = sess.getStore(urln);
if (showAlert) {
store.addStoreListener(new StoreListener() {
public void notification(StoreEvent e) {
String s;
if (e.getMessageType() == StoreEvent.ALERT)
s = "ALERT: ";
else
s = "NOTICE: ";
System.out.println(s + e.getMessage());
}
});
}
store.connect();

} else {
if (protocol != null)
store = sess.getStore(protocol);
else
store = sess.getStore();

// Connect
if (host != null || user != null || password != null)
store.connect(host, port, user, password);
else
store.connect();
}

// Open the Folder

Folder folder = store.getDefaultFolder();
if (folder == null) {
System.out.println("No default folder");
System.exit(1);
}

folder = folder.getFolder(mbox);
if (folder == null) {
System.out.println("Invalid folder");
System.exit(1);
}

// try to open read/write and if that fails try read-only
try {
folder.open(Folder.READ_WRITE);
} catch (MessagingException ex) {
folder.open(Folder.READ_ONLY);
}
int totalMessages = folder.getMessageCount();

if (totalMessages == 0) {
System.out.println("Empty folder");
folder.close(false);
store.close();
System.exit(1);
}
if (verbose) {
int newMessages = folder.getNewMessageCount();
System.out.println("Total messages = " + totalMessages);
System.out.println("New messages = " + newMessages);
System.out.println("-------------------------------");
}

if (msgnum == -1) {
// Attributes & Flags for all messages ..
Message[] msgs = folder.getMessages();

// Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(msgs, fp);

for (int i = 0; i < msgs.length; i++) {
System.out.println("--------------------------");
System.out.println("MESSAGE #" + (i + 1) + ":");
dumpEnvelope(msgs);
// dumpPart(msgs);
}
} else {
System.out.println("Getting message number: " + msgnum);
Message m = null;

try {
m = folder.getMessage(msgnum);
dumpPart(m);
} catch (IndexOutOfBoundsException iex) {
System.out.println("Message number out of range");
}
}

folder.close(false);
store.close();
}

%>
<%!

public static void dumpPart(Part p) throws Exception {
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);

**/

String ct = p.getContentType();
try {
pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
} catch (ParseException pex) {
pr("BAD CONTENT-TYPE: " + ct);
}
String filename = p.getFileName();
if (filename != null)
pr("FILENAME: " + filename);

/*
* 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)
System.out.println((String)p.getContent());
} else if (p.isMimeType("multipart/*")) {
pr("This is a Multipart");
pr("---------------------------");
Multipart mp = (Multipart)p.getContent();
level++;
int count = mp.getCount();
for (int i = 0; i < count; i++)
dumpPart(mp.getBodyPart(i));
level--;
} else if (p.isMimeType("message/rfc822")) {
pr("This is a Nested Message");
pr("---------------------------");
level++;
dumpPart((Part)p.getContent());
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("---------------------------");
System.out.println((String)o);
} else if (o instanceof InputStream) {
pr("This is just an input stream");
pr("---------------------------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
} else {
pr("This is an unknown type");
pr("---------------------------");
pr(o.toString());
}
} 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.
*/
if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) {
String disp = p.getDisposition();
// many mailers don't include a Content-Disposition
if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
if (filename == null)
filename = "Attachment" + attnum++;
pr("Saving attachment to file " + filename);
try {
File f = new File(filename);
if (f.exists())
// XXX - could try a series of names
throw new IOException("file exists");
OutputStream os =new BufferedOutputStream(new FileOutputStream(f));
InputStream is = p.getInputStream();
int c;
while ((c = is.read()) != -1)
os.write(c);
os.close();
} catch (IOException ex) {
pr("Failed to save attachment: " + ex);
}
pr("---------------------------");
}
}
}
%>
<%!
public static void dumpEnvelope(Message m) throws Exception {
pr("This is the message envelope");
pr("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
pr("FROM: " + a[j].toString());
}

// TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
pr("TO: " + a[j].toString());
}

// SUBJECT
pr("SUBJECT: " + m.getSubject());

// DATE
Date d = m.getSentDate();
pr("SendDate: " +
(d != null ? d.toString() : "UNKNOWN"));

// FLAGS
Flags flags = m.getFlags();
StringBuffer sb = new StringBuffer();
Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags

boolean first = true;
for (int i = 0; i < sf.length; i++) {
String s;
Flags.Flag f = sf;
if (f == Flags.Flag.ANSWERED)
s = "
Answered";
else if (f == Flags.Flag.DELETED)
s = "
Deleted";
else if (f == Flags.Flag.DRAFT)
s = "
Draft";
else if (f == Flags.Flag.FLAGGED)
s = "
Flagged";
else if (f == Flags.Flag.RECENT)
s = "
Recent";
else if (f == Flags.Flag.SEEN)
s = "
Seen";
else
continue; // skip it
if (first)
first = false;
else
sb.append(' ');
sb.append(s);
}

String[] uf = flags.getUserFlags(); // get the user flag strings
for (int i = 0; i < uf.length; i++) {
if (first)
first = false;
else
sb.append(' ');
sb.append(uf);
}
pr("FLAGS: " + sb.toString());

// X-MAILER
String[] hdrs = m.getHeader("X-Mailer");
if (hdrs != null)
pr("X-Mailer: " + hdrs[0]);
else
pr("X-Mailer NOT available");
}

%>
<%!
public static void pr(String s) {
if (showStructure)
System.out.print(indentStr.substring(0, level * 2));
System.out.println(s);
}

}

%>

</code>

Can anyone find out what is the mistake ?

 

Posts:31,095
Registered: 4/30/99
Re: Mail inbox code  
Jul 21, 2004 9:00 AM (reply 1 of 3)



 
The JSP compiler creates a Java class and compiles it. Any scriptlet code you put in your JSP goes inside a method (whose name I don't remember, but that's irrelevant). You can't declare static variables inside a method, hence the error message.

PC²
 

Posts:5,627
Registered: 3/22/04
Re: Mail inbox code  
Jul 21, 2004 9:11 AM (reply 2 of 3)



 
declarations <%! %> become class members.
scriptlets and expressions <% %> and <%= %> are method local to _jspService()
 

Posts:474
Registered: 3/12/04
Re: Mail inbox code  
Jul 21, 2004 9:35 AM (reply 3 of 3)



 
I removed static,actual problem was an extra } at the back.Now my explorer hangs,the bar moves and
nothing happens.When i press 'cancel' it becomes normal.
 
This topic has 3 replies on 1 page.