Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created March 12, 2013 21:28
Show Gist options
  • Save kentcdodds/5147233 to your computer and use it in GitHub Desktop.
Save kentcdodds/5147233 to your computer and use it in GitHub Desktop.
Download all e-mails in a specific folder into a single text file. Specific for Google Apps accounts.
package com.kentcdodds.gmailretriever.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
/**
*
* @author kentdodds
*/
public class Main {
private Store store;
private String username = "[email protected]",
password = "PASSWORD",
folderName = "FOLDER NAME",
outputFile = System.getProperty("user.home") + "/Desktop/" + folderName + ".txt",
nl = System.getProperty("line.separator");
private int limit = -1;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws MessagingException, IOException {
Main m = new Main();
m.connectToGoogle();
m.printFolderContents();
}
private void connectToGoogle() throws MessagingException {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imaps");
store.connect("imap.gmail.com", username, password);
}
private void printFolderContents() throws MessagingException, IOException {
Folder folder = store.getFolder(folderName);
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
File file = new File(outputFile);
System.out.println(file.getPath());
if (!file.exists()) {
file.createNewFile();
}
PrintWriter pw = new PrintWriter(file);
for (int i = 0; i < messages.length && (i < limit || limit < 0); i++) {
Message message = messages[i];
try {
String body = getMessageBody(message);
if (body == null) {
System.err.println("Null body with message: " + getMessageInfo(message));
} else {
System.out.println("Printing " + message.getSubject());
pw.println(getMessageInfo(message));
pw.println(nl + nl);
pw.println("-----------------------------------------------------------------------------");
pw.println(nl + nl);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
folder.close(false);
pw.close();
}
private String getMessageBody(Message message) throws MessagingException, IOException {
String type = message.getContentType();
String body = null;
if (type.contains("TEXT/PLAIN")) {
return (String) message.getContent();
}
if (type.contains("multipart")) {
MimeMultipart multipart = (MimeMultipart) message.getContent();
for (int i = 0; i < multipart.getCount(); i++) {
BodyPart bodyPart = multipart.getBodyPart(i);
String contentType = bodyPart.getContentType();
if (contentType.contains("TEXT/PLAIN")) {
return (String) bodyPart.getContent();
} else if (contentType.contains("TEXT/HTML")) {
body = (String) bodyPart.getContent();
}
}
}
return body;
}
private String getMessageInfo(Message message) throws MessagingException, IOException {
return "Subject: " + message.getSubject() + nl
+ "Date: " + message.getSentDate() + nl
+ "From: " + getAddressAsString(message.getFrom()) + nl
+ "To: " + getAddressAsString(message.getRecipients(Message.RecipientType.TO)) + nl
+ "Cc: " + getAddressAsString(message.getRecipients(Message.RecipientType.CC)) + nl
+ "Bcc: " + getAddressAsString(message.getRecipients(Message.RecipientType.BCC)) + nl
+ "Body:" + nl + nl
+ getMessageBody(message);
}
private String getAddressAsString(Address... addresses) {
if (addresses == null) {
return "";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < addresses.length; i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(addresses[i]);
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment