google code prettify

2016年2月21日 星期日

用javamail 取得mail資訊

最近在工作上遇到需要用程式抓取mail的相關資訊(收件者、寄件者、標題、內容…etc)
因為google到大多數都是採用javamail來實現,
而程式碼也相當簡單




package lab.charles.email.test;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailServer {

 public static void main(String[] args) throws Exception{
  // mail server connection parameters
     String host = "mail server";
     String user = "user name";
     String password = "password";
  
     // connect to my pop3 inbox
     Properties properties = System.getProperties();
     Session session = Session.getDefaultInstance(properties);
     Store store = session.getStore("pop3");
     store.connect(host, user, password);
     Folder inbox = store.getFolder("Inbox");
     inbox.open(Folder.READ_ONLY);
  
     // get the list of inbox messages
     Message[] messages = inbox.getMessages();
  
     if (messages.length == 0) System.out.println("No messages found.");
  
     for (int i = 0; i < messages.length; i++) {
       // stop after listing ten messages
       if (i > 10) {
         System.exit(0);
         inbox.close(true);
         store.close();
       }
  
       System.out.println("Message " + (i + 1));
       System.out.println("From : " + messages[i].getFrom()[0]);
       System.out.println("Subject : " + messages[i].getSubject());
       System.out.println("Sent Date : " + messages[i].getSentDate());
       System.out.println();
     }
  
     inbox.close(true);
     store.close();

 }

}


程式碼也可以至github下載