欢迎光临
我们一直在努力

改写后的使用javamail pop3收信的javabean-JSP教程,邮件相关

建站超值云服务器,限时71元/月

/*
*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!");
        }
%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 改写后的使用javamail pop3收信的javabean-JSP教程,邮件相关
分享到: 更多 (0)

相关推荐

  • 暂无文章