• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 22次 | 进入软件测试论坛讨论

领测软件测试网 /*
*Author:tyfun
*DateTime:2003.01.10
*Package:com.westarsoft.mail
*/

package com.westarsoft.mail;

import javax.mail.*;
import javax.mail.inte.net.*;
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!");
        }
%>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网