javaMail里有不少例子,我想试一下,怎么在我的计算机上预演?就象写的jsp程序在tomca上执行得到你所要的结果一样?请多帮忙

解决方案 »

  1.   

    POP3协议是不支持对新邮件的判断的,你可以用IMAP,但是前提是你的邮件服务器要支持该协议。如果只能用POP3的话那我以前的做法是把邮件的发送时间和邮件大小保存在本地,然后每次检查的时候一封封进行比较,如果不一样的就认为是新邮件,该方法在实际应用中发现误判率非常低。
      

  2.   

    这是个简单的例子,你自己改造改造应该没有问题了import java.io.*;
    import java.net.*;
    import java.util.*;public class Pop3Monitor implements Monitor {
         protected int port = 110;
        protected String hostname = "pop3.sina.com.cn";
        protected String username = "";//username
        protected String password = "";//password
        protected int timeLimit = 20000;   protected PrintWriter pw;
        public HashMap test() {
            HashMap returnMsg = new HashMap();
            try {
                // Get mail messages
                displayEmails(returnMsg);
            } catch (Exception e) {
                returnMsg.put("NetStatus", "Error");
            }        return returnMsg;
        }
        protected boolean responseIsOk() throws Exception {
            String line = br.readLine();
            System.out.println("< " + line);
            return line.toUpperCase().startsWith("+OK");
        }
        protected String readLine(boolean debug) throws Exception {
            String line = br.readLine();        if (debug) {
                System.out.println("< " + line);
            } else {
                System.out.println(line);
            }
            return line;
        }
        protected void writeMsg(String msg) throws Exception {
            pw.println(msg);
            pw.flush();
            System.out.println("> " + msg);
        }
        protected void closeConnection() throws Exception {
            pw.flush();
            pw.close();
            br.close();
            socket.close();
        }
        protected void sendQuit() throws Exception {
            System.out.println("Sending QUIT");
            writeMsg("QUIT");
            readLine(true);        System.out.println("Closing Connection");
            closeConnection();
        }
        protected void displayEmails(HashMap returnMsg) throws Exception {
            System.out.println("Opening Socket");
            try {
                long before = new Date().getTime();
                socket = new Socket(this.hostname, this.port);
                long after = new Date().getTime();
                returnMsg.put("connecttime", "" + (after - before));
            } catch (IOException ex) {
                returnMsg.put("connection", "failed");
                //throw new Exception("Invalid SMTP Address");
                return;
            }        br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));        long before = new Date().getTime();
            boolean responseOk = responseIsOk();
            long after = new Date().getTime();        if (!responseOk) {
                socket.close();
                returnMsg.put("POP3 Service", "Abnormal");
                return;
            }        returnMsg.put("ResponseTime", "" + (after - before));        System.out.println("Sending username");
            writeMsg("USER " + this.username);
            if (!responseIsOk()) {
                sendQuit();
                //throw new Exception("Invalid username");
                returnMsg.put("UserCheck", "Not Exit");
                return;
            }        System.out.println("Sending password");
            writeMsg("PASS " + this.password);
            if (!responseIsOk()) {
                sendQuit();
                //throw new Exception("Invalid password");
                returnMsg.put("PasswordCheck", "Not Correct");
                return;
            }        System.out.println("Checking mail");
            writeMsg("STAT");        String line = readLine(true);
            StringTokenizer tokens = new StringTokenizer(line, " ");
            tokens.nextToken();
            int messages = Integer.parseInt(tokens.nextToken());
            int maxsize = Integer.parseInt(tokens.nextToken());        if (messages == 0) {
                System.out.println("There are no messages.");
                sendQuit();
                return;
            }        returnMsg.put("POP3 Service","OK");
            sendQuit();
        }}
      

  3.   

    谢谢了,我刚开始学这东西,好多都不懂,再麻烦一下,请告诉我,这个例子是放在javamail里还是放那都行?用java,javac能编译测试不?
      

  4.   

    上海仙人,谢谢了,麻烦你把Monitor那个接口也给我呀,要不然我怎么测试呀!帮我帮到底嘛!十分感谢
      

  5.   

    判断是否是新邮件,javamail无法做到,
    我已经试过好多次了,javamail的文档里也提到
    目前无法实现判断是否是新邮件
      

  6.   

    帮人帮到底呀!麻烦把代码给全呀![email protected]
    谢谢,要不贴初来最好,让我们这些学习的人,好好的学习一下嘛!!!