关于用JAVA收邮件的问题
以下是详细代码,我在我自已机上装的邮件服务器,可以发邮件,但是今天写个收邮件的就一直报异常,
在网上找了很多资料,代码都差不多。
我是在servlet里写的,首先执行时默认调用doget()
方法,然后在doget()方法里写一个表单,表单提交方式是post,提交后调用本servlet的dopost()方法。
package zhidao1;
import javax.servlet.*;
import javax.servlet.http.*;
import *;
import java.util.*;
import javax.mail.*;
public class UserInfo extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>UserInfo</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<H1>输入详细信息以接收邮件</H1>");
out.println("<form action='userinfo' method='post'>");
out.println("<table width='296' height='144' border='0'>");
out.println("<tr><td width='81' height='46'>用户ID:</td><td width='239'><input type='text' name='t1'></td></tr>");
out.println("<tr><td>密 码:</td><td><input type='text' name='t2'></td></tr>");
out.println("<tr><td height='34'>主 机:</td><td><input type='text' name='t3'></td></tr>");
out.println("<tr><td> </td><td><input type='submit' name='Submit' value='提交'></td></tr>");
out.println("</table>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//request.setCharacterEncoding("GBK");
//response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String host = request.getParameter("t3");
String usename = request.getParameter("t1");
String password = request.getParameter("t2");
try{
Properties pro = System.getProperties();
pro.setProperty("mail.pop3s.rsetbeforequit","true");
pro.setProperty("mail.pop3.rsetbeforequit","true");
Session session = Session.getInstance(pro, null);
Store store = session.getStore("pop3");
store.connect(host,usename,password);
Folder folder = store.getFolder("INBOX");
// if(folder==null){
// throw new Exception("INBOX error");
// }
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
out.println("<table align='center'>");
out.println("<tr><td>MsgNO</td><td>发件人</td><td>主题</td><td>日期</td></tr>");
for(int i=0;i<message.length;i++){
out.println("<tr><td>"+(i+1)+"</td><td>"+message[i].getFrom()[0]+"</td><td>"+message[i].getSubject()+"</td><td>"+message[i].getSentDate().toString()+"</td></tr>");
}
out.println("</table>");
store.close();
}catch(Exception ex){
ex.printStackTrace();
}
out.close();
}
//Clean up resources
public void destroy() {
}
}
异常代码如下:
javax.mail.MessagingException: Connect failed;
nested exception is:
Connection refused: connect
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:106)
at javax.mail.Service.connect(Service.java:234)
at javax.mail.Service.connect(Service.java:135)
at zhidao1.UserInfo.doPost(UserInfo.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)