MailFetcher

乜承嗣
2023-12-01

package javamailtest;


import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

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

import com.sun.mail.pop3.POP3Folder;

/**
 * 邮件接收演示例子
 * @author liudong
 */
public class MailFetcher {

 /**
  * 使用默认的110端口收取邮件
  * @param host
  * @param user
  * @param password
  * @return
  * @throws IOException
  * @throws MessagingException
  */
 public static void listMails(String host,String user,String password)
   throws IOException, MessagingException {
  listMails(host,110,user,password);
 }
 /**
  * 接收指定帐号的所有邮件概要信息(不包括内容和附件)
  * @param account
  * @return
  * @throws MessagingException
  * @throws UnsupportedEncodingException
  */
 public static void listMails(String host,int port,String user,String password)
   throws IOException, MessagingException {
  //pop3必须小写
  URLName url = new URLName("pop3", host, port, "", user, password);
  Session session = Session.getDefaultInstance(System.getProperties(),null);
  Store store = session.getStore(url);
  POP3Folder inbox = null;
  try {
   store.connect();
   inbox = (POP3Folder) store.getFolder("INBOX");
   inbox.open(Folder.READ_ONLY);
   FetchProfile profile = new FetchProfile();
   profile.add(UIDFolder.FetchProfileItem.UID);
   profile.add(FetchProfile.Item.ENVELOPE);
   Message[] messages = inbox.getMessages();
   inbox.fetch(messages, profile);
   for (int i = 0; i < messages.length; i++) {
    //邮件发送者
    String from = decodeText(messages[i].getFrom()[0].toString());
    InternetAddress ia = new InternetAddress(from);
    System.out.println("FROM:"+ia.getPersonal());
    //邮件发送者地址
    System.out.println("FROM_ADDR:"+ia.getAddress());
    //邮件标题
    System.out.println("TITLE:"+messages[i].getSubject());
    //邮件的唯一标识信息
    System.out.println("UID:"+inbox.getUID(messages[i]));
    //邮件大小
    System.out.println("SIZE:"+messages[i].getSize());
    //邮件发送时间
    System.out.println("DATE:"+messages[i].getSentDate());
    //读取邮件内容
    Object content = messages[i].getContent();
    if(content instanceof String)
     System.out.println("CONTENT:"+content);
    else
    if(content instanceof Multipart)
     dumpMultipart((Multipart)content);
   }
  } finally {
   try{
    inbox.close(false);
   }catch(Exception e){}
   try{
    store.close();
   }catch(Exception e){}
  }
 }

 protected static String decodeText(String text) throws UnsupportedEncodingException{
  if(text==null)
   return null;
  if (text.startsWith("=?GB") || text.startsWith("=?gb"))
   text = MimeUtility.decodeText(text);
  else
   text = new String(text.getBytes("ISO8859_1"));
  return text;
 }

 protected static void dumpMultipart(Multipart mmp) throws MessagingException, IOException{

  //System.out.println("ContentType:"+mmp.getContentType());
  for(int pc=0;pc<mmp.getCount();pc++){
   BodyPart bp = mmp.getBodyPart(pc);
   Object content = bp.getContent();
   if(content instanceof String){
    System.out.println("CONTENT:"+content);
   }
   else
   if(content instanceof Multipart)
    dumpMultipart((Multipart)content);
   else
   if(content instanceof InputStream)
    System.out.println("FileName:"+decodeText(bp.getFileName()));
  }
 }

 public static void main(String[] args) throws IOException, MessagingException {
   listMails("pop3.163.com","lyws","800518");
 }
}

 

 类似资料:

相关阅读

相关文章

相关问答