Chinaunix首页 | 论坛 | 博客
  • 博客访问: 109836
  • 博文数量: 19
  • 博客积分: 600
  • 博客等级: 上士
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-12 04:04
文章分类

全部博文(19)

文章存档

2014年(4)

2009年(2)

2008年(13)

我的朋友

分类: Java

2008-01-20 02:19:54

返回结果是一个Message[],遍历 Message 可以得到邮件的具体信息.构造函数 为 GMAIL 的完整用户名
 
及密码.
 
 

package noname;

import java.security.Security;
import java.util.Properties;

import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

public class GoogleMail {

    protected final static String POP_HOST_NAME = "pop.gmail.com";
    protected final static int POP_PORT = 995;
    protected final static String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    protected String userName;
    protected String userPass;
    protected Message[] msg = null;
    public GoogleMail(String user,String pass)
    {
        userName = user;
        userPass = pass;
    }
    
    public Message[] connect()
    {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        Properties props = new Properties();
        props.put("mail.pop3.host", POP_HOST_NAME);
        props.put("mail.pop3.auth", "true");
        props.put("mail.pop3.port", POP_PORT);
        props.put("mail.pop3.socketFactory.port", POP_PORT);
        props.put("mail.pop3.socketFactory.class", SSL_FACTORY);
        Session session = Session.getDefaultInstance(props,null);
        URLName url = new URLName("pop3", POP_HOST_NAME, POP_PORT, null,userName,userPass);
        try {
            Store store = session.getStore(url);
            Folder inbox = null;
            store.connect();
            inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            FetchProfile profile = new FetchProfile ();
            profile.add(FetchProfile.Item.ENVELOPE);
            profile.add("X-mailer");
            msg = inbox.getMessages();
            inbox.fetch(msg, profile);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return msg;
    }    
}

阅读(2100) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~