ITeye博客

java电子邮件(三)

ShellyLi · 2009-09-02 15:33 · javamail · Java · SUN · Markdown

最新的JavaMail 1.3.1增强了在国际化字符集的处理,很多原来的关于中文的问题都可以迎刃而解,下面是一个例子用来收取邮箱中的所有邮件,包括内容附件等。经过试验:标题,内容,发件人名,中文附件都可以正确处理!大家测试的时候请下载最新的JavaMail包以及Java Activation Framework(JAF,activation.jar),然后需要修改一下在main方法中的邮箱地址以及帐号,代码如下: /* Created on 2004-7-5 / package demo; 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
      .getFrom().toString()); InternetAddress ia = new InternetAddress(from); System.out.println("FROM:"+ia.getPersonal()); //邮件发送者地址 System.out.println("FROM_ADDR:"+ia.getAddress()); //邮件标题 System.out.println("TITLE:"+messages*.getSubject()); //邮件的唯一标识信息 System.out.println("UID:"+inbox.getUID(messages*)); //邮件大小 System.out.println("SIZE:"+messages*.getSize()); //邮件发送时间 System.out.println("DATE:"+messages*.getSentDate()); //读取邮件内容 Object content = messages*.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("邮件服务器地址","帐号","密码"); } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。