javamail接收邮件的bean,可以正确的解析中文(四)

Pop3Bean.java是主要的代码

FileProperties.java读取配置文件的类

MailConstants.java指向配置文件的接口,可以不用

Pop3BeanTest.java测试程序

ReadEml.java读取eml文件的bean



读取配置文件的类

package org.jteam.jmail.test;


import org.jteam.jmail.pop3bean.*;
import org.jteam.jmail.util.ReadEml;


public class Pop3BeanTest
{

public static void main(String[] args) throws Throwable
{
 

    try
    {
        Pop3Bean mail;
     mail = new Pop3Bean();
     mail.getConn();
     System.out.println("Count of messages in folder: " + mail.getAllMessageCount());
     System.out.println("Count of new messages in folder: " + mail.getNewMessageCount());
     System.out.println("Count of unread messages in folder: " + mail.getUnreadMessageCount());
     //mail.getAllMessages();
     System.out.println("Count of loaded messages: " + mail.getMessageCount());
//     mail.getAllMail();
     mail.getAllMail();
     System.out.println(mail.parseMail(0).getSubject());
     mail.closeConnection();
//     ReadEml mail = new ReadEml("H:\\My_Soft_Works\\java\\jteam\\jmail\\received\\41C95D0F.008CD1.01099.eml");

    }
    catch(Exception e)
    {
     System.out.println("出现未预料的错误!");
     e.printStackTrace();
    }
}
}

读取eml文件的bean

/*
* 创建日期 2005-1-4
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package org.jteam.jmail.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;

/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class ReadEml {
      private ParseMessage pm = new ParseMessage();
      private MimeMessage message;
public ReadEml(String fileName) throws MessagingException, Exception {
          try {
              //TODO readEmlFile
              InputStream fis = new FileInputStream(fileName);
              Object emlObj = (Object)fis;
              Session mailSession = Session.getDefaultInstance(System.getProperties(), null);
              message = new MimeMessage(mailSession,fis);
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
/**
    * 用message初始化pm
    * @return
    */
public ParseMessage parseMail(){
          pm.setMessage(message);
    return pm;
}
}

你可能感兴趣的:(java,bean)