求救:JAVAMAIL编的EMAIL系统

04-12-20 lele7666
我是一名刚开始学习JAVAMAIL编程的新手,我照一本教科书上的做了个邮件系统,主要在接收带有附件的邮件时出了问题,源码如下:

//登陆
login.html
<HEAD>
<TITLE>MyMail</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<FORM Name="FM" ACTION="login.jsp" METHOD="POST">
<table width="55%" border="1" align="center"><tr>
<td width="37%">邮件主机名:</td>
<td width="63%"><INPUT NAME="host"></td>
</tr><tr>
<td width="37%">用户名:</td>
<td width="63%"><INPUT NAME="user"></td>
</tr><tr>
<td width="37%">密码o</td>
<td width="63%"><input type="password" name="password"></td>
</tr>
<tr><td colspan=2 align="center"><a href="javascript:FM.submit();">提交</a>
</td></tr>
</table>
</FORM>
</BODY></HTML>


login.jsp
<%
session.setAttribute("host",request.getParameter("host"));
session.setAttribute("user",request.getParameter("user"));
session.setAttribute("password",request.getParameter("password"));
response.sendRedirect("listfolder.jsp?action=list");
%>


//显示邮件列表的JSP页面
listfolder.jsp
<HTML><HEAD><TITLE>网络收件箱</TITLE>
<%@ page import=" javax.mail.*,java.util.*"
  contentType="text/html;charset=GB2312"%>
</head><body>
<%
String action=request.getParameter("action");
String msg=request.getParameter("msg");
if (action==null) {
	response.sendRedirect("login.html");
}else if (action.equals("logout")){
	response.sendRedirect("login.html");
}else{
	// Get a Properties object
	Properties props = System.getProperties();
	// Get a Session object
	Session foldersession = Session.getDefaultInstance(props, null);
	foldersession.setDebug(true);
	URLName urln = new URLName("pop3://"+session.getAttribute("user")+":"
		+session.getAttribute("password")+"@"+session.getAttribute("host")+"/");
	Store store = foldersession.getStore(urln);
	store.connect();

	Folder inbox = store.getFolder("INBOX");
	inbox.open(Folder.READ_WRITE);

	if (action.equals("delete")){
		if (msg!=null){
			int imsg=Integer.parseInt(msg);
			Message message = inbox.getMessage(imsg);
			if(message != null){
      			message.setFlag(Flags.Flag.DELETED,true);
      			inbox.close(true);
      		}
      	}
	      store.close();
	      out.print("删除成功<br>");
      	out.print("<A HREF=\"listfolder.jsp?action=list\">返回收件箱</A>");
	}else if (action.equals("list")){
		int count = inbox.getMessageCount();
%>
<A HREF="listfolder.jsp?action=list">收件箱</A>
<A HREF="listfolder.jsp?action=logout" >退出</A>
<%      		
		for(int i = 1;i <= count;i++){
		 	Message message = inbox.getMessage(i);
		 	if(!message.isSet(Flags.Flag.DELETED)) {
            String title = message.getSubject();
            String title2 = new String(title.getBytes("iso8859-1"), "gb2312");
%>
<form>
<p>[<%=i%>]<A HREF="detail.jsp?msg=<%=i%>">标题:<%=title2%></A>
<A HREF="listfolder.jsp?action=delete&msg=<%=i%>">删除</a>
</form>

		<%  }
		}
      inbox.close(true);
	   store.close();		
	}	
}
%>
</BODY></HTML>


//接收带有附件的Email
detail.jsp
<HTML><HEAD><TITLE>网络收件箱</TITLE>
<%@ page import=" javax.mail.*,java.util.*,java.io.*"
  contentType="text/html;charset=GB2312"%>
</head><body>
<A HREF="listfolder.jsp?action=list">收件箱</A>
<A HREF="listfolder.jsp?action=logout" >退出</A>
<%
	String msg=request.getParameter("msg");
	// Get a Properties object
	Properties props = System.getProperties();
	// Get a Session object
	Session foldersession = Session.getDefaultInstance(props, null);
	foldersession.setDebug(true);
	URLName urln = new URLName("pop3://"+session.getAttribute("user")+":"
		+session.getAttribute("password")+"@"+session.getAttribute("host")+"/");
	Store store = foldersession.getStore(urln);
	store.connect();

	Folder inbox = store.getFolder("INBOX");
	inbox.open(Folder.READ_WRITE);
	if (msg!=null){
		int imsg=Integer.parseInt(msg);
		Message message = inbox.getMessage(imsg);
		Address[] addresses;
%>
<table width=80% align="center">
<tr><td>发件人:</td><td>
<%
      addresses=message.getFrom();
      if(addresses != null)
         for(int i = 0;i < addresses.length;i++)
            out.print(addresses[i] + "&nbsp&nbsp");
      else
         out.println("无");
%>
</td></tr>
<tr><td>收件人:</td><td>
<%
      addresses=message.getRecipients(Message.RecipientType.TO);
      if(addresses != null)
         for(int i = 0;i < addresses.length;i++)
            out.print(addresses[i] + "&nbsp&nbsp");
      else
         out.println("无");
%>
</td></tr>
<tr><td>抄送:</td><td>
<%
      addresses=message.getRecipients(Message.RecipientType.CC);
      if(addresses != null) {
         for(int i = 0;i < addresses.length;i++) {
            out.print(addresses[i] + "&nbsp&nbsp");
         }
      }
      else {
         out.println("无");
      }
      String title = message.getSubject();
      String title2 = new String(title.getBytes("iso8859-1"), "gb2312");
%>
</td></tr>
<tr><td>标题:</td><td><%=title2%></td></tr>
<tr><td colspan=2>
<%
if(null == request.getParameter("part")){
	if(message.isMimeType("multipart/*")){
    	Multipart multipart = (Multipart)message.getContent();
         for(int i = 0;i < multipart.getCount();i++){
            Part p = multipart.getBodyPart(i);
            String disposition = p.getDisposition();
            if ((disposition != null) &&
            (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))) {
               String filename = p.getFileName();
%>
<br><A HREF="detail.jsp?msg=<%=message.getMessageNumber()%>&part=<%=i%>">附件</A>
<%
            }else if (disposition==null){
               if (p.isMimeType("text/plain")) {
                  out.print(p.getContent());
              }else {
                //特殊附件
            }
         }
      }
   } else if(message.isMimeType("text/plain")) {
     out.print(message.getContent());
   }
} else {
	int partnr = Integer.parseInt(request.getParameter("part"));
	Part p;
	if(partnr < 0)
		p = message;
	else{
		Multipart multipart = (Multipart)message.getContent();
		p = multipart.getBodyPart(partnr);
	}
	response.setContentType(p.getContentType());
	if (p.getFileName()!=null){
		response.setHeader("Content-Disposition", "attachment; filename=\"" + p.getFileName() + "\"");
	}
	OutputStream os = response.getOutputStream();
	InputStream is = p.getInputStream();
	int c = is.read();
	while(c != -1) {
		os.write(c); c = is.read();
	}
}
%>
</td></tr>
</table>
<%	}
	inbox.close(true);
	store.close();
%>
</BODY></HTML>
<p>

我想请教的问题是:listfolder.jsp中第7行"String msg=request.getParameter("msg");中的这"msg"以及detail.jsp中的

if(null == request.getParameter("part"))中的“part"从何而来??

请各位老师赐教!我感激不尽!

banq
2004-12-25 13:21
我说句极端的话,在jsp中嵌入这些Java代码,这些是“垃圾”代码,很少有人能够或愿意看出“part"从何而来”。不如用delphi来编。

使用Struts+JMS架构来重新写这些代码。

javer
2004-12-31 18:16
牛 能把代码写成这样 不简单

ballr
2006-02-15 12:59
汗啊,我的代码也是这么个风格写的~~~~不好意思啊,得改

猜你喜欢