?? mailfetcher.java
字號(hào):
package acceptmail;
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 {
/**
* 使用默認(rèn)的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);
}
/**
* 接收指定帳號(hào)的所有郵件概要信息(不包括內(nèi)容和附件)
* @port 默認(rèn)25
* @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類提供對(duì)文件夾的訪問方法并驗(yàn)證連接
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類包含標(biāo)題和內(nèi)容
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
for (int i = 0; i < messages.length; i++) {
//郵件發(fā)送者
String from = decodeText(messages[i].getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:"+ia.getPersonal());
//郵件發(fā)送者地址
System.out.println("FROM_ADDR:"+ia.getAddress());
//郵件標(biāo)題
System.out.println("TITLE:"+messages[i].getSubject());
//郵件的唯一標(biāo)識(shí)信息
System.out.println("UID:"+inbox.getUID(messages[i]));
//郵件大小
System.out.println("SIZE:"+messages[i].getSize());
//郵件發(fā)送時(shí)間
System.out.println("DATE:"+messages[i].getSentDate());
//讀取郵件內(nèi)容
Object content = messages[i].getContent();
//instanceof 運(yùn)算符返回一個(gè) boolean 值,指出對(duì)象是否是特定類的一個(gè)實(shí)例。
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;
}
//郵件國(guó)際化
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","zhongdi163","123456");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -