熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用javamail自動獲取gmail郵件

2013-11-23 19:36:16  來源: Java核心技術 

  /*
   
    Fetching email by IMAP
   
    prerequisite: IMAPjar
   
    */
   
    import javaio*;
   
    import javautil*;
   
    import javaxmail*;
   
    import javaxmailFlagsFlag;
   
    import javaxmailinternet*;
   
    import javaxmailsearch*;
   
    import comsunmailimapIMAPFolder;
   
    import comsunmailimapIMAPMessage;
   
    import javaioPrintWriter;
   
    import javaioFileWriter;
   
    import javaioIOException;
   
    public class FolderFetchIMAP {
   
    PrintWriter pw;
   
    PrintWriter pw;
   
    int num = ;
   
    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
   
    {
   
    Properties props = SystemgetProperties()
   
    propssetProperty(mailstoreprotocol imaps
   
    Session session = SessiongetDefaultInstance(props null)
   
    store = sessiongetStore(imaps
   
    nnect(username password)
   
    folder = (IMAPFolder) storegetFolder(inbox
   
    if(!folderisOpen())
   
    folderopen(FolderREAD_ONLY)
   
    //Message[] messages = foldergetMessages()
   
    //SearchTerm st = new SentDateTerm(SentDateTermGE getSearchDate(date))
   
    SearchTerm st = new SentDateTerm(SentDateTermGT getSearchDate(date))
   
    Message[] messages = foldersearch(st)
   
    Systemoutprintln(Num of Messages : + messageslength)
   
    for (int i=; i < messageslength;i++)
   
    {
   
    Systemoutprintln(*****************************************************************************
   
    Systemoutprintln(MESSAGE + (i + ) +
   
    Message msg =  messages[i];
   
    String[] ymd = datesplit(
   
    int month day;
   
    Date d= msggetReceivedDate()
   
    month = IntegerparseInt(ymd[]);
   
    day = IntegerparseInt(ymd[])
   
    // should care about Timezone!!!
   
    if(month!= (dgetMonth()) || day != dgetDay()) {
   
    Systemoutprintln(Skipped
   
    continue;
   
    }else
   
    num++;
   
    try{
   
    pw = new PrintWriter(new FileWriter(path+/+num+month++day+txt))
   
    pw = new PrintWriter(new FileWriter(path+/+num+month++day+txt))
   
    }catch(IOException e) {
   
    eprintStackTrace()
   
    }
   
    Systemoutprintln(Writing Head…
   
    pwprintln(Subject: + msggetSubject())
   
    pwprintln(From: + msggetFrom()[])
   
    pwprintln(To: +msggetAllRecipients()[])
   
    pwprintln(Date: +msggetReceivedDate())
   
    pwprintln(
   
    try {
   
    Systemoutprintln(Writing Content…
   
    getMailContent(msg)
   
    } catch (Exception e) {
   
    eprintStackTrace()
   
    return;
   
    }
   
    pwclose()
   
    pwclose()
   
    }
   
    }catch(Exception e){
   
    eprintStackTrace()
   
    }
   
    finally
   
    {
   
    if (folder != null && folderisOpen()) { folderclose(true) }
   
    if (store != null) { storeclose() }
   
    }
   
    }
   
    public void getMailContent(Part part) throws Exception {
   
    StringBuffer bodytext = new StringBuffer()
   
    String contenttype = partgetContentType()
   
    int nameindex = contenttypeindexOf(name
   
    boolean conname = false;
   
    if (nameindex !=
   
    conname = true;
   
    if ( (partisMimeType(text/plain)||(partisMimeType(text/html))) && !conname) {
   
    bodytextappend((String) partgetContent())
   
    pwprintln(bodytexttoString())
   
    } else if (partisMimeType(multipart/*)) {
   
    Multipart multipart = (Multipart) partgetContent()
   
    int counts = multipartgetCount()
   
    for (int i = ; i < counts; i++) {
   
    getMailContent(multipartgetBodyPart(i))
   
    }
   
    } else if (partisMimeType(message/rfc)) {
   
    getMailContent((Part)partgetContent())
   
    } else if(conname && partisMimeType(text/plain)) {
   
    // if conname is nonemptyis it an attachment?
   
    bodytextappend((String)partgetContent())
   
    Systemoutprintln(Writing attachment…
   
    pwprintln(bodytexttoString())
   
    }
   
    }
   
    private Date getSearchDate(String date) {
   
    String[] ymd = datesplit(
   
    int year month day;
   
    Date srchdate = null;
   
    try {
   
    year = IntegerparseInt(ymd[])
   
    month = IntegerparseInt(ymd[]);
   
    day = IntegerparseInt(ymd[])
   
    Calendar cal = CalendargetInstance()
   
    calset(year month day)
   
    srchdate = calgetTime()
   
    } catch (Exception ex) {
   
    exprintStackTrace()
   
    }
   
    return srchdate;
   
    }
   
    public static void main(String[] args) throws MessagingException IOException {
   
    FolderFetchIMAP mail = new FolderFetchIMAP()
   
    if(argslength==) {
   
    mailpath=args[];
   
    mailfetchMail(username passwordargs[]) // date string(eg )as the first parameter
   
    }
   
    }
   
    }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/27160.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.