JavaMail Client on the NetBeans Platform (Step 1)

Let's create the simple JavaMail client, first mentioned in yesterday's blog entry. Once we have it, we'll be able to analyze it and see how best to port it to the NetBeans Platform. The simple client comes from the JavaMail download, so we'll just get it from there and then get it up and running.

  1. Download JavaMail here. Next, download the Java Activation Framework here.

  2. Use the Java Project with Existing Sources template, to create an application from the sources at demo/client in the unzipped JavaMail download. Refactor the default package, where all the sources end up, to a package name like "client". Move simple.mailcap to the project root (use the Files window to do this, putting the simple.mailcap file in the same place where build.xml is found).

  3. Use the Library Manager to create a library where you add the JAR files for JavaMail and the Java Activation Framework. Attach that library to your application's Libraries node.

    You should now see this in the Projects window:

    You can only see simple.mailcap if you look in the Files window.

  4. Right-click the application in the Projects window, choose Properties and add something like this in the Run tab's Arguments field:

    -L imap://

    Doesn't have to be imap. See JavaMail docs for other supported protocols.

  5. Run the project. If the Output window shows error messages about 'login methods not being found' (which is a rather obscure message), see this tip:

    How do I connect to an IMAP server over an SSL connection?

    Once the application is successfully deployed, you'll be able to view your e-mails in your simple client. However, I encountered the same NPE as someone else (here). It starts like this:

    at MessageViewer.getBodyComponent(
    at MessageViewer.setMessage(

    This happens when I want to open a message in the lower part of the client. However, for other types of messages, there's no problem. Need to investigate this. Anyway, for the successfully opened messages, the client looks as follows:

Now that we have a simple client, we are able to examine its content and see how best to port it to the NetBeans Platform, described in part 2.



do you also know the mailfs module from contrib? it was for NB 3.x I think. It was an example from the older NetBeans book "Definitive Guide". Here is the book and the examples:

The Mail file system is on page 382 of the pdf file.

Posted by Ramon on January 30, 2007 at 03:21 AM PST #

Ramon, thanks a lot! I didn't know about that module, and it looks like it will be really useful. What I am doing here, though, is show how to port an application to the NetBeans Platform. It just so happens that it is a mail client. But I will try and build that mail client from the Definitive Guide (and update where necessary to 5.x) and see how much I can reuse.

Posted by Geertjan on January 30, 2007 at 04:20 PM PST #

Hello Geertjan, I followed everything and it compiles in Netbeans and even runs successful, I even found missing servlets which I hijacked from Enterprise plugins. However no IDE appears. Regards, Barry

Posted by Barry Jaques on April 06, 2007 at 03:36 PM PDT #

Sorry, trouble with my setup of simple.mailcap. Everything works as stated with the NPE included.

Posted by Barry Jaques on April 06, 2007 at 09:35 PM PDT #

Good to hear, Barry. Now continue with part 2 of this series!

Posted by Geertjan on April 07, 2007 at 03:31 AM PDT #

Hello Geertjan, I have decided to start this tutorial series again from scratch. I have successfully compiled and run the client. I retrieved email from gmail and read 3 different types of messages (text, html and multipart) all with no problems or NPE. I could not connect to Gmail using the standard connection code in the demo client (even with the additions in your SimpleClient)so I tweaked the code with stuff I found in GMailUtility It must be stated that when I did this again with a new project I did get the NPE so I will have to study the codes to see if I can spot any difference. Know of any software that does this, like diff in c++. I am now on my way to part 2, see you there.

Posted by Michael Kunta on July 14, 2007 at 11:45 PM PDT #


Posted by stiti on August 11, 2007 at 03:39 AM PDT #


Posted by marco on September 29, 2007 at 01:06 AM PDT #

How does one go about getting javamail to show up in the Netbeans libraries list for the project? I've done the obvious add/open dialog thing. However, upon selecting the library and "OK"ing the dialog box, nothing happens. Dragging the file manually into the file structure makes Netbeans hang up while scanning. What am I missing?

Posted by Brian Knoblauch on November 12, 2008 at 09:33 PM PST #

Hi Brian, use the Library Manager under the Tools menu.

Posted by Geertjan on November 12, 2008 at 09:35 PM PST #

RE: How do I connect to an IMAP server over an SSL connection?

Hi, there is an easier way to connect via SSL: change the protocol from imap to imaps (e.g., -L imaps://

I have not tested this with an IMAP server, but at least it works for my POP3 SSL Gmail account (by changing the protocol from pop3:// to pop3s://).

This is documented in Library / imap.jar (and pop3.jar) / META-INFO / javamail.providers file.


Posted by Tom Doan on October 02, 2010 at 03:16 PM PDT #

hey can u give this whole project in .zip or .rar bcs when i implement your method i found some error so me give full project in one file...

Posted by guest on September 25, 2011 at 06:13 AM PDT #

I'm trying with gmail with this setting
-L imap://

but the output when i'm running the program is like this:
what's wrong?

javax.mail.MessagingException: Connection timed out: connect;
nested exception is: Connection timed out: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at javax.mail.Service.connect(
at StoreTreeNode.loadChildren(
at StoreTreeNode.getChildCount(
at javax.swing.tree.DefaultTreeModel.getChildCount(
... 39 more

Posted by guest on November 22, 2011 at 12:48 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016