求救:JAVAMAIL編的EMAIL系統

lele7666發表於2004-12-20
我是一名剛開始學習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 class="indent">[<%=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 class="indent">

我想請教的問題是:listfolder.jsp中第7行"String msg=request.getParameter("msg");中的這"msg"以及detail.jsp中的
if(null == request.getParameter("part"))中的“part"從何而來??
請各位老師賜教!我感激不盡!


相關文章