谁有Javamail的收邮件的例子,要能够运行的。比如可以收pop3.163.com上的。。我已经作了好几天了。救救我吧。我可以给你200分。

解决方案 »

  1.   

    呵呵,这也是我前一段时间从参考了好多大侠的代码编成的,Mail是一个bean,然后从jsp文件中调用这个bean//接收并阅读邮件的bean
    package xx;import java.io.*;
    import java.util.Properties;
    import javax.mail.*;
    import javax.mail.internet.*;
    import java.util.*;public class Mail{
    Store store;
        Folder folder;
    Message message[];
     
     public void init(String host,String username,String password) throws Exception{
    Session session =Session.getInstance(new Properties(),null);
       //get the store
       store=session.getStore("pop3");
       store.connect(host,username,password);
       //get folder
       folder = store.getFolder("INBOX");
       folder.open(Folder.READ_ONLY);
      
       //get directory
       message = folder.getMessages();
       }
       
      public MailParts[] getList() throws Exception{
        try{
    int mailCnt = message.length;
    MailParts[] rM = new MailParts[mailCnt];
    for(int i=0; i<mailCnt; i++){
      rM[i] = new MailParts();
      //System.out.println(message[i].getContentType());
      String ctp = message[i].getContentType();
      rM[i].id = message[i].getMessageNumber();
      if(ctp.indexOf("GB2312") != -1){
       rM[i].decode = "GB2312";
    rM[i].subject = new String(message[i].getSubject().getBytes("8859_1"),"GBK");
      }else
    rM[i].subject = message[i].getSubject(); //显示乱码,需要有以下代码进行编码
      
      //rM[i].subject = new String(message[i].getSubject().getBytes("8859_1"),"GBK");
      //System.out.println(message[i].getEncoding());
      rM[i].from = message[i].getFrom()[0].toString();
      rM[i].recDate = message[i].getSentDate();//getReceivedDate()  取不出时间来
      //rM[i].recDate = message[i].getReceivedDate(); 
      Object content=message[i].getContent();
      rM[i].attach = 0;
      if(content instanceof Multipart){ //如果邮件有附件或者邮件是html邮件
       for(int k=0;k<((Multipart)content).getCount();k++){
    Part thePart = ((Multipart)content).getBodyPart(k);
    String dispos = thePart.getDisposition();
    if(dispos!=null && dispos.equals(Part.ATTACHMENT)) //如果有附件
    rM[i].attach ++;
    }
      }
    }
    return rM;
      }catch(Exception e){
       e.printStackTrace();
       System.out.println(e.toString());
       return null;
      }
      }    
     
     public String getText(int mailId) throws MessagingException,IOException{
       Object content = message[mailId].getContent();
       String theTxt = "";
       if(content instanceof Multipart){
        //System.out.println("操作一次!");
    theTxt = getMultipart((Multipart)content);
    }else{
    //System.out.println("再操作一次!");
            theTxt = getPart(message[mailId]);
    }
    return theTxt;
    }public String[] getAttName(int mailId,int attCnt) throws MessagingException,IOException{
       Object content = message[mailId].getContent();
       Multipart mContent = (Multipart)content;
       String[] theAttName = new String[attCnt];
       int k = 0;
       for(int i=0,n=mContent.getCount();i<n;i++){
      Part mPart = mContent.getBodyPart(i);
    String disposition=mPart.getDisposition();
    if(disposition==null) continue;
    else if(disposition.equals(Part.ATTACHMENT)){
    String atTempName = mPart.getFileName();
    theAttName[k] = new String(atTempName.getBytes("8859_1"),"GBK");
    k ++;
    //String atName = new String(atTempName.getBytes("8859_1"),"GB2312");
    //String atName = new String(atTempName.getBytes("GBK"),"ISO-8859-1");
    }else continue;   
       }
       
       return theAttName;
    }
     
     
     String getMultipart(Multipart multipart) throws MessagingException,IOException{
         String txtStr = "";
     for(int i=0,n=multipart.getCount();i<n;i++){
      //System.out.println("第"+i+"次操作!");
    txtStr = txtStr + "<br><font color=red>第" + i + "部分正文内容:</font>" + getPart(multipart.getBodyPart(i));
         }
     return txtStr;
     }
     
     String getPart(Part part) throws MessagingException,IOException{
        String disposition=part.getDisposition();
        String contentType=part.getContentType();
    String theBody = "";    
     System.out.println("进入了getPart模块!");
     //System.out.println(contentType);
    try{
     if(disposition==null){//当前处理部分为正文时
        //check if plain
    if((contentType.length()>=10) && (contentType.toLowerCase().substring(0,10).equals("text/plain"))){
            theBody = (String)part.getContent();
         }else{//当邮件正文中含有html内容时
              //theBody = "Other body:" + contentType;
              //theBody ="<br>" + part.getContent();
    Object thisPart = part.getContent();
    if(thisPart instanceof MimeMultipart){
    theBody = "这是MimeMultpart<br>";
    theBody = theBody + "共有"+ ((MimeMultipart)thisPart).getCount()+"部分";
    for(int l=0;l<((MimeMultipart)thisPart).getCount();l++){
    theBody = theBody + "<br>第" + l +"部分:";
    theBody = theBody + (((MimeMultipart)thisPart).getBodyPart(l)).getContent();
    }

    }else theBody = "<br>" + thisPart;
         }
         }else if(disposition.equals(Part.ATTACHMENT)){
    String atTempName = part.getFileName();
    String atName = new String(atTempName.getBytes("8859_1"),"GBK");
    //String atName = new String(atTempName.getBytes("8859_1"),"GB2312");
    //String atName = new String(atTempName.getBytes("GBK"),"ISO-8859-1");
    theBody = "这部分是附件,附件名:" + atName;
    theBody = theBody + "<br>附件的类型:" + contentType;
    /*下面代码是试验中文用何种编码解决
    String s=part.getFileName();
    try{
    System.out.println(s);
    System.out.println(new String(s.getBytes("GB2312"),"8859_1"));
    System.out.println(new String(s.getBytes("8859_1"),"GB2312"));
    }catch(Exception ex){}
    */
    //System.out.println("contentType:" + contentType); 

          //saveFile(part.getFileName(),part.getInputStream());
    //saveFile(atName,part.getInputStream());
         }else if(disposition.equals(Part.INLINE)){
          theBody = "\"InLine\"的文件:" + part.getFileName() + "<br>类型:"+contentType;
         }else{//should never happen
            theBody = "Other:" + disposition;
         }
    }catch(Exception e){
    System.out.println(e.toString());
    System.out.println(e.getMessage());
    e.printStackTrace();
    }
     return theBody;
      }
       
      public static void saveFile(String filename,InputStream input)throws IOException{
         if(filename==null){
          filename=File.createTempFile("xx","out").getName();
     }
         //如果目标目录中已有相同名字的附件,则在本附件名后加上相应数字
         String filepath = "attachment\\";
     File file= new File(filepath + filename);
         for(int i=0;file.exists();i++){
          file=new File(filepath + filename + i);
         }
     //***********************
     
         FileOutputStream fos=new FileOutputStream(file);
         BufferedOutputStream bos=new BufferedOutputStream(fos);
         BufferedInputStream bis = new BufferedInputStream(input);
         int aByte;
         while((aByte=bis.read())!=-1){
          bos.write(aByte);
         }
         bos.flush();
         bos.close();
         bis.close(); 
     }
     
     
    public void closeConn() throws Exception{
      //close connection
      folder.close(false);
      store.close();
    }
    }
    /*总结:
    当邮件有附件或为html格式时,其message.content 为Multipart
    1)当无附件为html格式时,MultPart的第0部分为文本"text/plain",第1部分为html内容"text/html"
    2)当有附件为text格式时,Multpart的第0部分为文本,第1部分为附件
    3)当既有附件又为html格式时,Multpart的第0部分为正文(包括了text和html)第一部分开始为附件
    这时的part为MimePart
    */
      

  2.   

    上面是bean,里面有很多代码是我加上测试的,请分清
    下面是收邮件的jsp文件<%@ page contentType="text/html;charset=gb2312" %>
    <%@ page import="java.util.*,fym.mail_out.*" %>
    <HTML>
    <HEAD>
    <title>邮件列表</title>
    <style>
    <!--
    A:link {color:blue;}
    A:visited {color:#dd8866;}
    A:active {color:red;text-decoration:underline;}-->
    </style>
    </HEAD>
    <BODY bgcolor="#ffffff" text="#0000ff" topmargin="0" marginwidth="0" marginheight="0" >
    <div align="center"><font size=4>外部邮件(Internet)试验系统:邮件列表 </font></div>
      <%!
    int PageSize = 15;
    int CurPage = 1;
    int TotalCnt = 0;
    int PageCnt = 0;
    %>
    <jsp:useBean id="theMail" scope="session" class="fym.mail_out.Mail"/>
    <form name=mlist>
      <font size="3">
    <%
    String hostname = "pop.163.com";String username = "xxxx";
    String password = "xxxx";session.setAttribute("hostname",hostname);
    session.setAttribute("username",username);
    session.setAttribute("password",password);String mailflag = "rec";
    MailParts[] mP = null;
    try{
    theMail.init(hostname,username,password);
    mP = theMail.getList();
    TotalCnt = mP.length;
    }
    catch(Exception m){
    m.printStackTrace();
    out.print(m.toString());
    if(m.toString().indexOf("Connect failed")!=-1)
    out.print("解析邮件主机名错误,可能是网络不通,或IP设置问题,请检查!");
    return;
    }
    //MailPart是我自己定义的一个类,包括了邮件主题等信息package fym.mail_out;public class MailParts {
    public int id;           //1
    public String subject;   //2
    public String from;    //3
    public java.util.Date recDate;  //4
    public int attach;     //5 附件数目
    public String decode;   //6 邮件的编码方式
    }
      

  3.   

    注意,上一个jsp文件没有贴出邮件主题的显示,很简单,你从mP数组中可以提取任何你想要的邮件信息加以显示,然后,通过链接传递邮件的id到阅读邮件的页面如下:这是读邮件的内容,我做的比较粗,只是一股恼地把所有邮件内容显示出来了,附件也是把文件名显示出来,只是test,但稍加完善就可以实用的,注意都是调用的同一个bean里的不同方法,并且username和pwd是从session里读来的,你当然可以现赋值了<%@ page contentType="text/html;charset=gb2312" %>
    <%@ page import="java.util.*,fym.mail_out.*" %>
    <HTML>
    <HEAD>
    <title>读邮件</title>
    </HEAD>
    <BODY bgcolor="#ffffff" text="#0000ff" topmargin="0" marginwidth="0" marginheight="0" >
    <div align="center"><font size=4>外部邮件(Internet)试验系统:阅读邮件内容 </font></div>
    <jsp:useBean id="theMail" scope="session" class="fym.mail_out.Mail"/>
    <form name=mlist>
      <font size="3">
    <%
    String idStr = request.getParameter("mailid");
    //这应该是从上一个列邮件的jsp文件中提交过来的
    int mId = Integer.parseInt(idStr);
    /*
    String hostname = (String)session.getAttribute("hostname");
    String username = (String)session.getAttribute("username");
    String password = (String)session.getAttribute("password");
    */
    String mailBody = "";
    try{
    //theMail.init(hostname,username,password);
    mailBody = theMail.getText(mId);
    }
    catch(Exception m){
    //out.print(m.toString());
    if(m.toString().indexOf("Connect failed")!=-1)
    out.print("解析邮件主机名错误,可能是网络不通,或IP设置问题,请检查!");
    return;
    }
    %>
    <%=mailBody %>
    </body>     
    </HTML>