网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Java编程 >> 文章正文
  改寫後的使用javamail pop3收信的javabean            【字体:
改寫後的使用javamail pop3收信的javabean
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-2    
正在装载数据……
 /*
*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!");
        }
%> 


本文来源:http://blog.csdn.net/jdgdg/archive/2007/07/08/1682613.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     用java实现web服务器
     用java快速开发linux gu…
     正则表达式分解siemens交…
     [portal参考手册]目录
     jsp中调用oracle存储过程…
  • 快速、简便使用AJAX技术的三…

  • [ZT]关于用jar打包(分享)

  • Ant入门-配置和使用     选…

  • ant使用简介

  • java现状----编程使用的语言…

  • Cookie又见Cookie-使用Html…

  • 一个关于Comparator的使用例…

  • 关于JSP的隐式对象的使用(供…

  • 使用ICE遇到的编译问题

  • Solaris10下,使用SunStudio…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David