gmail.java
来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 142 行
JAVA
142 行
package com.vegeta.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;
import javax.mail.internet.MimeUtility;
public class GMail {
public static void main(String argv[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
Session session = Session.getDefaultInstance(props, null);
URLName urln = new URLName("pop3", "pop.gmail.com", 995, null,
"cadic195@gmail.com", "tinhban195");
Store store = session.getStore(urln);
Folder inbox = null;
try {
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
System.out.println("Inbox Number of Messages: " + messages.length);
for (int i = 0; i < messages.length; i++) {
// Part part = (Part) messages[i].getContent();
//
// System.out.println(part.getContent().toString());
filter(messages[i]);
}
} 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;
}
/**
* The filter() method tries to figure out the message type, and recursively
* sends multi-part parts through the filter.
*
* @param part
* The message part.
*
* @return True iff successful.
*/
@SuppressWarnings("unchecked")
public static boolean filter(Part part) {
Object o = null;
try {
for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();) {
Header header = (Header) e.nextElement();
System.out.println(header.getName() + ": " + header.getValue());
}
System.out.println("\n");
o = part.getContent();
} catch (Exception e) {
System.out.println("Couldn't get content.");
return false;
}
if (o instanceof String) {
String lMessage = (String) o;
System.out.println(lMessage);
return true;
} else if (o instanceof Multipart) {
Multipart lPart = (Multipart) o;
try {
int count = lPart.getCount();
for (int i = 0; i < count; i++)
return filter(lPart.getBodyPart(i));
} catch (Exception e) {
System.out.println("Couldn't access parts.");
e.printStackTrace();
}
} else if (o instanceof Message) {
return filter((Part) o);
} else if (o instanceof InputStream) {
BufferedReader lReader = new BufferedReader(new InputStreamReader(
(InputStream) o));
int c;
StringBuffer lBuffer = new StringBuffer();
try {
while ((c = lReader.read()) != -1)
lBuffer.append((char) c);
lReader.close();
} catch (Exception e) {
System.out.println("Read error.");
e.printStackTrace();
}
System.out.println(lBuffer.toString());
return true;
}
return false;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?