会计考友 发表于 2012-8-4 12:44:44

JAVA高级编程:使用javamail自动获取gmail邮件

/*
   
    Fetching email by IMAP
   
    prerequisite: IMAP.jar
   
    */
   
    import java.io.*;
   
    import java.util.*;
   
    import javax.mail.*;
   
    import javax.mail.Flags.Flag;
   
    import javax.mail.internet.*;
   
    import javax.mail.search.*;
   
    import com.sun.mail.imap.IMAPFolder;
   
    import com.sun.mail.imap.IMAPMessage;
   
    import java.io.PrintWriter;
   
    import java.io.FileWriter;
   
    import java.io.IOException;
   
    public class FolderFetchIMAP {
   
    PrintWriter pw1;
   
    PrintWriter pw2;
   
    int num = 0;
   
    public String path;
   
    private void fetchMail(String username, String password, String date) throws MessagingException, IOException{
   
    IMAPFolder folder = null;
   
    Store store = null;
   
    String subject = null;
   
    Flag flag = null;
   
    try

会计考友 发表于 2012-8-4 12:44:45

JAVA高级编程:使用javamail自动获取gmail邮件

{
   
    Properties props = System.getProperties();
   
    props.setProperty("mail.store.protocol", "imaps");
   
    Session session = Session.getDefaultInstance(props, null);
   
    store = session.getStore("imaps");
   
    store.connect("imap.googlemail.com",username, password);
   
    folder = (IMAPFolder) store.getFolder("inbox");
   
    if(!folder.isOpen())
   
    folder.open(Folder.READ_ONLY);
   
    //Message[] messages = folder.getMessages();
   
    //SearchTerm st = new SentDateTerm(SentDateTerm.GE, getSearchDate(date));
   
    SearchTerm st = new SentDateTerm(SentDateTerm.GT, getSearchDate(date));
   
    Message[] messages = folder.search(st);
   
    System.out.println("Num of Messages : " + messages.length);
   
    for (int i=0; i < messages.length;i++)
   
    {
   
    System.out.println("*****************************************************************************");
   
    System.out.println("MESSAGE " + (i + 1) + "…");
   
    Message msg =messages;
   
    String[] ymd = date.split("-");
   
    int month, day;
   
    Date d= msg.getReceivedDate();
   
    month = Integer.parseInt(ymd)-1;
   
    day = Integer.parseInt(ymd);
   
    // should care about Timezone!!!
   
    if(month!= (d.getMonth()) || day != d.getDay()) {
   
    System.out.println("Skipped");
   
    continue;
   
    }else
   
    num++;
   
    try{

会计考友 发表于 2012-8-4 12:44:46

JAVA高级编程:使用javamail自动获取gmail邮件

pw1 = new PrintWriter(new FileWriter(path+"/"+num+month+"-"+day+".txt"));
   
    pw2 = new PrintWriter(new FileWriter(path+"/"+num+month+"-"+day+".txt"));
   
    }catch(IOException e) {
   
    e.printStackTrace();
   
    }
   
    System.out.println("Writing Head…");
   
    pw2.println("Subject: " + msg.getSubject());
   
    pw2.println("From: " + msg.getFrom());
   
    pw2.println("To: "+msg.getAllRecipients());
   
    pw2.println("Date: "+msg.getReceivedDate());
   
    pw2.println("--------------------");
   
    try {
   
    System.out.println("Writing Content…");
   
    getMailContent(msg);
   
    } catch (Exception e) {
   
    e.printStackTrace();
   
    return;
   
    }
   
    pw1.close();
   
    pw2.close();
   
    }
   
    }catch(Exception e){
   
    e.printStackTrace();
   
    }
   
    finally
   
    {
   
    if (folder != null && folder.isOpen()) { folder.close(true); }
   
    if (store != null) { store.close(); }
   
    }
   
    }
   
    public void getMailContent(Part part) throws Exception {
   
    StringBuffer bodytext = new StringBuffer();
   
    String contenttype = part.getContentType();
   
    int nameindex = contenttype.indexOf("name");
   
    boolean conname = false;
   
    if (nameindex != -1)
   
    conname = true;
   
    if ( (part.isMimeType("text/plain")||(part.isMimeType("text/html"))) && !conname) {
   
    bodytext.append((String) part.getContent());
   
    pw2.println(bodytext.toString());
   
    } else if (part.isMimeType("multipart/*")) {
   
    Multipart multipart = (Multipart) part.getContent();
   
    int counts = multipart.getCount();

会计考友 发表于 2012-8-4 12:44:47

JAVA高级编程:使用javamail自动获取gmail邮件

for (int i = 0; i < counts; i++) {
   
    getMailContent(multipart.getBodyPart(i));
   
    }
   
    } else if (part.isMimeType("message/rfc822")) {
   
    getMailContent((Part)part.getContent());
   
    } else if(conname && part.isMimeType("text/plain")) {
   
    // if conname is non-empty,is it an attachment?
   
    bodytext.append((String)part.getContent());
   
    System.out.println("Writing attachment…");
   
    pw1.println(bodytext.toString());
   
    }
   
    }
   
    private Date getSearchDate(String date) {
   
    String[] ymd = date.split("-");
   
    int year, month, day;
   
    Date srchdate = null;
   
    try {
   
    year = Integer.parseInt(ymd);
   
    month = Integer.parseInt(ymd)-1;
   
    day = Integer.parseInt(ymd);
   
    Calendar cal = Calendar.getInstance();
   
    cal.set(year, month, day);
   
    srchdate = cal.getTime();
   
    } catch (Exception ex) {
   
    ex.printStackTrace();
   
    }
   
    return srchdate;
   
    }
   
    public static void main(String[] args) throws MessagingException, IOException {
   
    FolderFetchIMAP mail = new FolderFetchIMAP();
   
    if(args.length==2) {
   
    mail.path=args;
   
    mail.fetchMail("username", "password",args); // date string(eg. 2012-07-07)as the first parameter
   
    }
   
    }
   
    }
页: [1]
查看完整版本: JAVA高级编程:使用javamail自动获取gmail邮件