Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxdna committed Dec 4, 2011
0 parents commit d52648f
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
@@ -0,0 +1 @@
Saleem Ansari - tuxdna(at)gmail(dot)com
3 changes: 3 additions & 0 deletions LICENSE
@@ -0,0 +1,3 @@
Released under Apache License 2.0 which can be found at
http://www.opensource.org/licenses/apache2.0.php

30 changes: 30 additions & 0 deletions README.txt
@@ -0,0 +1,30 @@
Build requirements:
* maven2
* jdk6

How to build?

$ mvn package


How to run?

$ java -jar target/mymail-0.0.1-SNAPSHOT-jar-with-dependencies.jar
usage: org.tuxdna.mail.FetchMail
-b fetch message body
-h <arg> host
-i check only folder: inbox
-u <arg> username

Yahoo Account: myusername@yahoo.com
-h imap.mail.yahoo.com -u myusername

Gmail Account: myusername@gmail.com
-h imap.googlemail.com -u myusername@gmail.com


Sample run:

$ java -jar target/mymail-0.0.1-SNAPSHOT-jar-with-dependencies.jar -h imap.mail.yahoo.com -u yahoo_user_name -i
[Password:] ..enter..password..

67 changes: 67 additions & 0 deletions pom.xml
@@ -0,0 +1,67 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.tuxdna</groupId>
<artifactId>mymail</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MyMail</name>

<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.10</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.tuxdna.mail.app.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
177 changes: 177 additions & 0 deletions src/main/java/org/tuxdna/mail/FetchMail.java
@@ -0,0 +1,177 @@
package org.tuxdna.mail;

import java.io.Console;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class FetchMail {
private static long start = System.currentTimeMillis();
private static String FOLDER_INVALID = "folder cannot contain messages";
private static String CONNECTION_FAILURE = "connection failure";

private static void help(Options options) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(FetchMail.class.getName(), options);
}

private static void info(String s) {
System.out.println(s);
}

private static void timeIt(String s) {
long end = System.currentTimeMillis();
System.out.println(s + " - time:" + (end - start) + "ms");
start = end;
}

public static void main(String[] args) {

// create Options object
Options options = new Options();
options.addOption("u", true, "username");
options.addOption("h", true, "host");
options.addOption("b", false, "fetch message body");
options.addOption("i", false, "check only folder: inbox");

CommandLineParser parser = new PosixParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.exit(-1);
}

String username = null;
String host = "imap.gmail.com";
boolean fetchBody = false;
boolean fetchOnlyInbox = false;

if (cmd.hasOption("u")) {
username = cmd.getOptionValue("u");
} else {
help(options);
System.exit(-1);
}

if (cmd.hasOption("h")) {
host = cmd.getOptionValue("h");
}

if (cmd.hasOption("b")) {
fetchBody = true;
}

if (cmd.hasOption("i")) {
fetchOnlyInbox= true;
}

Console cons = null;
char[] passwd = null;
if ((cons = System.console()) != null
&& (passwd = cons.readPassword("[%s]", "Password:")) != null) {
} else {
System.err.println("Reading password failed!");
System.exit(-1);
}

String password = new String(passwd);
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");

try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
info("Connecting to server: " + host);
store.connect(host, username, password);
info("Connected.");

Folder default_folder = store.getDefaultFolder();

info("Searching for all folders");
for (Folder folder : default_folder.list()) {
String folder_name = folder.getName();
info("Opening folder: " + folder_name);

if(fetchOnlyInbox &&
! folder_name.equalsIgnoreCase("inbox") ) {
info("this is not inbox, skipping it...");
continue;
}

try {
start = System.currentTimeMillis();
folder.open(Folder.READ_WRITE);
info("Opened.");
timeIt("Opening folder ");
int count = folder.getMessageCount();
System.out.println("Total messages: " + count);
timeIt("Getting folder count ");
info("Getting all messages now... be patient");
if (fetchBody) {
info("I will also fetch the body part of each message...");
}

int batch_size = 10;
for (int msg_id = 1; msg_id <= count; msg_id += batch_size) {
int l = msg_id;
int u = (msg_id + batch_size - 1) < count ? msg_id
+ batch_size - 1 : count;
Message messages[] = folder.getMessages(l, u);
for (Message m : messages) {
Enumeration<Header> headers = m.getAllHeaders();
while (headers.hasMoreElements()) {
Header h = headers.nextElement();
}
if (fetchBody) {
try {
Object m_obj = m.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
timeIt("Getting messages from " + l + " to " + u);
}
} catch (MessagingException e) {

String msg = e.getMessage();
if (msg.equals(FOLDER_INVALID)) {
info("Perhaps an invalid folder");
} else if (msg.equals(CONNECTION_FAILURE)) {
info("Connection failed. consider RECONNECT.");
} else {
info("Unknow failure");
e.printStackTrace();
}
}

info(""); // just a blank line
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/tuxdna/mail/app/App.java
@@ -0,0 +1,9 @@
package org.tuxdna.mail.app;

import org.tuxdna.mail.FetchMail;

public class App {
public static void main(String[] args) {
FetchMail.main(args);
}
}

0 comments on commit d52648f

Please sign in to comment.