改写后的使用javamail pop3收信的javabean

80酷酷网    80kuku.com

  /*
*Author:tyfun
*DateTime:2003.01.10
*Package:com.westarsoft.mail
*/

package com.westarsoft.mail;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;

public class GetMail {
    public static String receive(String popServer, String popUser, String popPassword) {
        String mailContent = "";
        Store store = null;
        Folder folder = null;
        try {
            Properties props = System.getProperties();
            Session session = Session.getDefaultInstance(props, null);
            store = session.getStore("pop3");
            store.connect(popServer, popUser, popPassword);
            folder = store.getDefaultFolder();
            if(folder == null) throw new Exception("No default folder");
            folder = folder.getFolder("INBOX");
            if(folder == null) throw new Exception("No POP3 INBOX");
            folder.open(Folder.READ_ONLY);
            Message[] msgs = folder.getMessages();
            for(int msgNum = 0; msgNum < msgs.length; msgNum++) {
                mailContent = mailContent + getMessage(msgs[msgNum]) + "\n\n\n\n";
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            try {
                if (folder!=null) folder.close(false);
                if (store!=null) store.close();
            }
            catch (Exception ex2) {
                ex2.printStackTrace();
            }
        }
        return mailContent;
    }

    public static String getMessage(Message message) {
        String mailContent = null;
        try {
            String from = ((InternetAddress)message.getFrom()[0]).getPersonal();
            if(from==null) from = ((InternetAddress)message.getFrom()[0]).getAddress();
            mailContent = "FROM: "+from;
            String subject = message.getSubject();
            mailContent = mailContent + "\n" +"SUBJECT: "+subject;
            Part messagePart = message;
            Object content = messagePart.getContent();
            if(content instanceof Multipart) {
                messagePart = ((Multipart)content).getBodyPart(0);
                mailContent = mailContent + "\n" +"[ Multipart Message ]";
            }
            mailContent = mailContent + "\n" +"CONTENT: "+content.toString();
            String contentType = messagePart.getContentType();
            mailContent = mailContent + "\n" +"CONTENT:"+contentType;
            if(contentType.startsWith("text/plain") || contentType.startsWith("text/html")) {
                InputStream is = messagePart.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String thisLine = reader.readLine();
                while(thisLine!=null) {
                    mailContent = mailContent + "\n" +thisLine;
                    thisLine = reader.readLine();
                }
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        return mailContent;
    }    
}





<%
        GetMail mail = new GetMail();
        String content = mail.receive("pop3.server.com","user","password");
        if((content.trim() == null)||(content.trim() == "")) {
            System.out.println("No mail!");
        }
        else {
            System.out.println("You got a new mail!");
        }
%>



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: