Java Mail (JavaMail) IMAP FAQ: Can you show me how to get a list of all the email addresses in an IMAP mailbox using the Java Mail API (JavaMail API)?
Sure. Here's the source code for a Java mail (JavaMail) program that extracts all of the "from" fields out of a specified mailbox. This works for both POP3 and IMAP mailboxes.
Before going to the code, note the cool use of a TreeSet
(java.util.TreeSet
) in this example. Based on our offline discussion, you mentioned that you really want is this:
- Only the unique email addresses, and
- The addresses sorted alphabetically.
Therefore, a TreeSet is a great data type for this purpose.
My JavaMail IMAP email address list program
With no further ado, here is the source code for my JavaMail email address list/extractor:
import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; /** * My JavaMail email address extractor. * A JavaMail API example. * @author alvin alexander, alvinalexander.com. */ public class AddressExtractor { public static void main(String[] args) { Properties props = new Properties(); String host = args[0]; String username = args[1]; String password = args[2]; //String provider = "pop3"; String provider = "imap"; try { //Connect to the server Session session = Session.getDefaultInstance(props, null); Store store = session.getStore(provider); store.connect(host, username, password); //open the inbox folder Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); // get a list of javamail messages as an array of messages Message[] messages = inbox.getMessages(); TreeSet treeSet = new TreeSet(); for(int i = 0; i < messages.length; i++) { String from = getFrom(messages[i]); if ( from!=null) { from = removeQuotes(from); treeSet.add(from); } } Iterator it = treeSet.iterator(); while ( it.hasNext() ) { System.out.println("from: " + it.next()); } //close the inbox folder but do not //remove the messages from the server inbox.close(false); store.close(); } catch (NoSuchProviderException nspe) { System.err.println("invalid provider name"); } catch (MessagingException me) { System.err.println("messaging exception"); me.printStackTrace(); } } private static String getFrom(Message javaMailMessage) throws MessagingException { String from = ""; Address a[] = javaMailMessage.getFrom(); if ( a==null ) return null; for ( int i=0; i<a.length; i++ ) { Address address = a[i]; from = from + address.toString(); } return from; } private static String removeQuotes(String stringToModify) { int indexOfFind = stringToModify.indexOf(stringToModify); if ( indexOfFind < 0 ) return stringToModify; StringBuffer oldStringBuffer = new StringBuffer(stringToModify); StringBuffer newStringBuffer = new StringBuffer(); for ( int i=0, length=oldStringBuffer.length(); i<length; i++ ) { char c = oldStringBuffer.charAt(i); if ( c == '"' || c == '\'' ) { // do nothing } else { newStringBuffer.append(c); } } return new String(newStringBuffer); } }
JavaMail IMAP list example - Discussion
Besides the TreeSet and JavaMail stuff going on, the removeQuotes()
method may also be of interest to you. This method removes single- and double-quotes from the email address. During testing, I ran into situations where one email address was *almost* the same as another address, except for cases where one email address had a name embedded in single-quotes, and the other address had no quotes or double-quotes. Because both addresses are technically the same for what I needed, I added this method to strip out all quotes.
Note that you will also need the following JavaMail API jar files for this Java email extractor class to work:
mail.jar
activation.jar
These jar files can be downloaded from Sun's web site. Just look for the JavaMail API on their site for instructions on obtaining these Jar files.
Comments
ask about autoreply mail with javamail
haow to build code in order to make autorespon mail with javamail.
what i need to make that code, mybe library or what
thanks,
net.kido
Very interesting
Very interesting application, I hadn't thought about this. You should be able to use a combination of (1) the techniques shown above to read the mailbox, and (2) this Java "send mail" class that I wrote to simplify the process of sending email. (I hope that helps.)