Created
June 12, 2011 16:18
-
-
Save taliesinb/1021717 to your computer and use it in GitHub Desktop.
Use JavaMail to download email headers from GMail and spit out TSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Properties; | |
import javax.mail.Folder; | |
import javax.mail.Message; | |
import javax.mail.MessagingException; | |
import javax.mail.NoSuchProviderException; | |
import javax.mail.Session; | |
import javax.mail.Store; | |
import javax.mail.FetchProfile; | |
public class InboxReader | |
{ | |
public static String combine(String[] s, String glue) | |
{ | |
int k=s.length; | |
if (k==0) return null; | |
StringBuilder out=new StringBuilder(); | |
out.append(s[0]); | |
for (int x=1;x<k;++x) | |
out.append(glue).append(s[x]); | |
return out.toString(); | |
} | |
public static void main(String args[]) { | |
Properties props = System.getProperties(); | |
props.setProperty("mail.store.protocol", "imaps"); | |
try { | |
Session session = Session.getDefaultInstance(props, null); | |
Store store = session.getStore("imaps"); | |
store.connect("imap.gmail.com", "USERNAME", "PASSWORD"); | |
System.out.println(store); | |
Folder inbox = store.getFolder("Inbox"); | |
inbox.open(Folder.READ_ONLY); | |
Message messages[] = inbox.getMessages(); | |
FetchProfile fp = new FetchProfile(); | |
inbox.fetch(messages, fp); | |
for (int i = 0; i < messages.length; i++) { | |
String from = messages[i].getFrom(); | |
String subject = messages[i].getSubject(); | |
String to = messages[i].getHeader("X-Mailer"); | |
System.out.println(from + "\t" + to + "\t" + subject); | |
} | |
} catch (NoSuchProviderException e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
System.exit(2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment