Javamailを使用してメールを受信する

7005 ワード

このプログラムはSun社が提供するjavamail 1に基づいています.1.4,簡単なメール受信機能を実現し,添付ファイル名の文字化けし問題を処理した.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
 
  
public class Getmail {
	private Properties props;
	private String dir = "D:" + File.separator +"Mail" + File.separator;
	boolean ssl;
	
	public Getmail(){
		props = new Properties();
		this.setProps();
		
	}
	
 
   public static void main(String args[]) throws MessagingException,   
           IOException {   
       
        
	   		Getmail mail = new Getmail();
	   		mail.parseMail();
    }  
   
	public void parseMail(){
		try {
			Session session = Session.getInstance(props);
			Store store = session.getStore("pop3");   
			store.connect("","");   //           
			Folder folder = store.getFolder("INBOX"); 
			System.out.println(folder.exists());
			folder.open(Folder.READ_ONLY); 
			
			Message[] messages = folder.getMessages();
			
			for(Message message:messages){
				System.out.println("  :" + this.getSubject(message));
				System.out.println("    :" + this.getSentTime(message));
				this.parseContent((Multipart)message.getContent());
			}

		} catch (NoSuchProviderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         
	}
    /**
     *     
     */
    private void setProps(){
    	
    	if(ssl){
    		this.handleSSL(props);
    	}
    	
         props.setProperty("mail.store.protocol", "pop3");   
         props.setProperty("mail.pop3.host", "pop.163.com");  
         
        // props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
         props.setProperty("mail.pop3.socketFactory.fallback", "false");
         props.setProperty("mail.pop3.port", "110");
         props.setProperty("mail.pop3.socketFactory.port", "110");
    }
    
    
    private String getSubject(Message message) throws MessagingException{
    	return message.getSubject();
    }
    
    private Date getSentTime(Message message) throws MessagingException{
    	return message.getSentDate();
    }
    
    /**
     * @param props
     */
    private void handleSSL(Properties props){
    	Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    	final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";  
        props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);  
        props.setProperty("mail.pop3.socketFactory.fallback", "false");  
        props.setProperty("mail.pop3.port", "995");  
        props.setProperty("mail.pop3.socketFactory.port", "995");  
  
        props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);  
        props.setProperty("mail.imap.socketFactory.fallback", "false");  
        props.setProperty("mail.imap.port", "993");  
        props.setProperty("mail.imap.socketFactory.port", "993"); 
    }
    
    /**
     *       
     * @param multipart
     */
    private void parseContent(Multipart multipart){
    	
    	try {
    		for(int i = 0;i<multipart.getCount();i++){
        		BodyPart message = multipart.getBodyPart(i);
    		if((message.isMimeType("text/html"))||(message.isMimeType("text/plain"))){
				System.out.println("    :" + message.getContent());
			}
    		if(message.isMimeType("multipart/*")){
    			Multipart msg = (Multipart)message.getContent();
    			parseContent(msg);
    		}
    		if(message.isMimeType("application/octet-stream")){
    			String disposition = message.getDisposition();
    			if(disposition == null || disposition.equalsIgnoreCase(Part.ATTACHMENT)){
    				saveToFile(message);
    			}
    		}
    		}
			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    } 
    
    /**
     *      
     * @param message
     */
    private void saveToFile(BodyPart message){
    	InputStream is = null;
    	OutputStream os = null;
    	File file = new File(dir);
    	if(!file.exists()&&!file.isDirectory()){
    		file.mkdirs();
    	}
    	try {
			String fileName = message.getFileName();
			
			fileName = MimeUtility.decodeText(fileName);		//         
			
			System.out.println(fileName);
			File output = new File(dir + fileName);

			is = message.getInputStream();
			os = new FileOutputStream(output);
			byte[] bytes = new byte[1024];
			 int len = 0;   
		        while ((len=is.read(bytes)) != -1 ) {   
		            os.write(bytes, 0, len);   
		        }  
			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				
				os.flush();
				is.close();
				os.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				
		}
    	
    }
}