/*
Fetching email by IMAP
prerequisite: IMAP
*/
import java
import java
import javax
import javax
import javax
import javax
import com
import com
import java
import java
import java
public class FolderFetchIMAP {
PrintWriter pw
PrintWriter pw
int num =
public String path;
private void fetchMail(String username
IMAPFolder folder = null;
Store store = null;
String subject = null;
Flag flag = null;
try
{
Properties props = System
props
Session session = Session
store = session
nnect(
folder = (IMAPFolder) store
if(!folder
folder
//Message[] messages = folder
//SearchTerm st = new SentDateTerm(SentDateTerm
SearchTerm st = new SentDateTerm(SentDateTerm
Message[] messages = folder
System
for (int i=
{
System
System
Message msg = messages[i];
String[] ymd = date
int month
Date d= msg
month = Integer
day = Integer
// should care about Timezone!!!
if(month!= (d
System
continue;
}else
num++;
try{
pw
pw
}catch(IOException e) {
e
}
System
pw
pw
pw
pw
pw
try {
System
getMailContent(msg)
} catch (Exception e) {
e
return;
}
pw
pw
}
}catch(Exception e){
e
}
finally
{
if (folder != null && folder
if (store != null) { store
}
}
public void getMailContent(Part part) throws Exception {
StringBuffer bodytext = new StringBuffer()
String contenttype = part
int nameindex = contenttype
boolean conname = false;
if (nameindex !=
conname = true;
if ( (part
bodytext
pw
} else if (part
Multipart multipart = (Multipart) part
int counts = multipart
for (int i =
getMailContent(multipart
}
} else if (part
getMailContent((Part)part
} else if(conname && part
// if conname is non
bodytext
System
pw
}
}
private Date getSearchDate(String date) {
String[] ymd = date
int year
Date srchdate = null;
try {
year = Integer
month = Integer
day = Integer
Calendar cal = Calendar
cal
srchdate = cal
} catch (Exception ex) {
ex
}
return srchdate;
}
public static void main(String[] args) throws MessagingException
FolderFetchIMAP mail = new FolderFetchIMAP()
if(args
mail
mail
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27160.html