返回结果是一个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) |