利用JavaMail实现邮件的收取

     昨天写了一个利用JavaMail发送邮件的示例,本着有始有终的原则。今天写了一篇利用JavaMail收取邮件的示例。由于实力有限,代码写的不周到的地方,请大家见亮。本人只是写了一个简单的例子,在实际项目开发中,还有很多东西需要读者自己去斟酌和修改。废话不多说,直接上代码。

1、一些参数配置的常量类

package com.bao.receivemail;

/**
 * 邮件配置的常量类
 */
public class Config {
	
	public static String MAIL_HOST = "pop3.163.com";//服务器ip
	
	public static int MAIL_PORT = 110;//端口
	
	public static String MAIL_TYPE = "pop3";//服务类型
	
	public static String MAIL_AUTH = "true";
	
	public static String MAIL_ATTACH_PATH = "upload/recMail/";//附件存放目录
}

2、邮件的收取类

package com.bao.receivemail;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import com.sun.mail.pop3.POP3Message;

/**
 * 邮件的收取类
 */
public class ReceiveMailHandler {
    
	/**
	 * 获取session会话的方法
	 * @return
	 * @throws Exception
	 */
    private Session getSessionMail() throws Exception {
    	Properties properties = System.getProperties();
    	properties.put("mail.smtp.host", Config.MAIL_HOST);
    	properties.put("mail.smtp.auth", Config.MAIL_AUTH);
    	Session sessionMail = Session.getDefaultInstance(properties, null);
	    return sessionMail;
    }
    
    /**
	 * 接收邮件
	 * @param 邮箱的用户名和密码
	 * @return 无
	 */
	public void receiveMail(String userName,String passWord) {
    	Store store = null;
    	Folder folder = null;
    	int messageCount = 0;
    	URLName urln = null;
		try{
			//进行用户邮箱连接
			urln = new URLName(Config.MAIL_TYPE, Config.MAIL_HOST, Config.MAIL_PORT, null,userName,passWord);   
			store = getSessionMail().getStore(urln);   
			store.connect();
			//获得邮箱内的邮件夹Folder对象,以"只读"打开
			folder = store.getFolder("INBOX");//打开收件箱
			folder.open(Folder.READ_ONLY);//设置只读
			//获得邮件夹Folder内的所有邮件个数
			messageCount = folder.getMessageCount();// 获取所有邮件个数
	        //获取新邮件处理
			System.out.println("============>>邮件总数:"+messageCount);
			if(messageCount > 0){
				Message[] messages = folder.getMessages(messageCount,messageCount);//读取最近的一封邮件
				for(int i = 0;i < messages.length;i++) {	
					String content = getMailContent((Part)messages[i]);//获取内容
				    if (isContainAttach((Part)messages[i])) {
	                	saveAttachMent((Part)messages[i],Config.MAIL_ATTACH_PATH);
	                } 
					System.out.println("=====================>>开始显示邮件内容<<=====================");
		            System.out.println("发送人: " + getFrom(messages[i]));
		            System.out.println("主题: " + getSubject(messages[i]));
		            System.out.println("内容: " + content);
		            System.out.println("发送时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(((MimeMessage) messages[i]).getSentDate()));
	                System.out.println("是否有附件: " + (isContainAttach((Part)messages[i]) ? "有附件" : "无附件"));
		            System.out.println("=====================>>结束显示邮件内容<<=====================");
		            ((POP3Message) messages[i]).invalidate(true);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(folder != null && folder.isOpen()){
				try {
					folder.close(true);
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
			if(store.isConnected()){
				try {
					store.close();
				} catch (MessagingException e) {
					e.printStackTrace();
				}
			}
		}
    }
   
	/**
     * 获得发件人的地址
     * @param message:Message
     * @return 发件人的地址
     */
	private String getFrom(Message message) throws Exception {  
        InternetAddress[] address = (InternetAddress[]) ((MimeMessage) message).getFrom();    
        String from = address[0].getAddress();    
        if (from == null){
        	from = "";
        }
        return from;    
    }
	
	 /**
     * 获得邮件主题   
     * @param message:Message
     * @return 邮件主题  
     */
	private String getSubject(Message message) throws Exception {
    	String subject = "";
    	if(((MimeMessage) message).getSubject() != null){
    		subject = MimeUtility.decodeText(((MimeMessage) message).getSubject());// 将邮件主题解码  
    	}
    	return subject;    
    }
	
    /**
     * 获取邮件内容
     * @param part:Part
     */
	private String getMailContent(Part part) throws Exception {    
		StringBuffer bodytext = new StringBuffer();//存放邮件内容
		//判断邮件类型,不同类型操作不同
		if (part.isMimeType("text/plain")) {    
            bodytext.append((String) part.getContent());    
        } else if (part.isMimeType("text/html")) {    
            bodytext.append((String) part.getContent());    
        } else if (part.isMimeType("multipart/*")) {    
            Multipart multipart = (Multipart) part.getContent();    
            int counts = multipart.getCount();    
            for (int i = 0; i < counts; i++) {    
                getMailContent(multipart.getBodyPart(i));    
            }    
        } else if (part.isMimeType("message/rfc822")) {    
            getMailContent((Part) part.getContent());    
        } else {}    
        return bodytext.toString();
    }
    
    /**
     * 判断此邮件是否包含附件 
     * @param part:Part
     * @return 是否包含附件
     */
	private boolean isContainAttach(Part part) throws Exception { 
        boolean attachflag = false;    
        if (part.isMimeType("multipart/*")) {    
            Multipart mp = (Multipart) part.getContent();    
            for (int i = 0; i < mp.getCount(); i++) {    
                BodyPart mpart = mp.getBodyPart(i);    
                String disposition = mpart.getDisposition();    
                if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE))))    
                    attachflag = true;    
                else if (mpart.isMimeType("multipart/*")) {    
                    attachflag = isContainAttach((Part) mpart);    
                } else {    
                    String contype = mpart.getContentType();    
                    if (contype.toLowerCase().indexOf("application") != -1)    
                        attachflag = true;    
                    if (contype.toLowerCase().indexOf("name") != -1)    
                        attachflag = true;    
                }    
            }    
        } else if (part.isMimeType("message/rfc822")) {    
            attachflag = isContainAttach((Part) part.getContent());    
        }    
        return attachflag;    
    }	
	
	 /**
     * 保存附件
     * @param part:Part
     * @param filePath:邮件附件存放路径
     */
	private void saveAttachMent(Part part,String filePath) throws Exception {    
    	String fileName = "";
    	//保存附件到服务器本地
        if (part.isMimeType("multipart/*")) {    
            Multipart mp = (Multipart) part.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
            	BodyPart mpart = mp.getBodyPart(i);    
                String disposition = mpart.getDisposition();   
                if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE)))) {   
                	fileName = mpart.getFileName();    
                	if (fileName != null) {
                    	fileName = MimeUtility.decodeText(fileName);
                        saveFile(fileName, mpart.getInputStream(),filePath);  
                    }  
                } else if (mpart.isMimeType("multipart/*")) {    
                	saveAttachMent(mpart,filePath);
                } else {
                	fileName = mpart.getFileName();    
                	if (fileName != null) {
                    	fileName = MimeUtility.decodeText(fileName);
                        saveFile(fileName, mpart.getInputStream(),filePath);
                    }    
                }    
            }    
        } else if (part.isMimeType("message/rfc822")) {    
            saveAttachMent((Part) part.getContent(),filePath);    
        }
        
    }
    
    /**
     * 保存附件到指定目录里 
     * @param fileName:附件名称
     * @param in:文件输入流
     * @param filePath:邮件附件存放基路径
     */
    private void saveFile(String fileName, InputStream in,String filePath) throws Exception {    
    	File storefile = new File(filePath);   
        if(!storefile.exists()){
        	storefile.mkdirs();
    	}
        BufferedOutputStream bos = null;   
        BufferedInputStream bis = null;   
        try {   
            bos = new BufferedOutputStream(new FileOutputStream(filePath + "\\" + fileName));   
            bis = new BufferedInputStream(in);   
            int c;   
            while ((c = bis.read()) != -1) {   
                bos.write(c);   
                bos.flush();   
            }   
        } catch (Exception e) {   
        	throw e;   
        } finally {
        	if(bos != null){
        		bos.close();
        	}
            if(bis != null){
            	bis.close();
            }
        }   
    } 
}

 3、测试程序

package com.bao.main;

import com.bao.receivemail.ReceiveMailHandler;

public class Main {

	public static void main(String[] args) {
		try {
			new ReceiveMailHandler().receiveMail("username", "password");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

运行结果会在控制台打印出来,只读取了邮箱中最新的那一封邮件。如果有附件,附件会自动保存到项目中你设置的文件目录中。程序不上传了,大家自己看下吧!谢谢大家的支持^.^!

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值