java

位置:IT落伍者 >> java >> 浏览文章

使用javamail自动获取gmail邮件


发布日期:2020年08月07日
 
使用javamail自动获取gmail邮件

/*

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

}

}

}

               

上一篇:如何为Java程序中添加播放MIDI音乐功能

下一篇:java高级技术总结 2