Geertjan's Blog

  • January 30, 2007

JavaMail Client on the NetBeans Platform (Step 1)

Geertjan Wielenga
Product Manager
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://gw12345:password@my-mail-server.com

    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(MessageViewer.java:199)
    at MessageViewer.setMessage(MessageViewer.java:104)

    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.

Join the discussion

Comments ( 13 )
  • Ramon Tuesday, January 30, 2007

    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.

  • Geertjan Wednesday, January 31, 2007
    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.
  • Barry Jaques Friday, April 6, 2007
    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.
  • Barry Jaques Saturday, April 7, 2007
    Sorry, trouble with my setup of simple.mailcap. Everything works as stated with the NPE included.
  • Geertjan Saturday, April 7, 2007
    Good to hear, Barry. Now continue with part 2 of this series!
  • Michael Kunta Sunday, July 15, 2007
    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.
  • stiti Saturday, August 11, 2007


  • marco Saturday, September 29, 2007


  • Brian Knoblauch Thursday, November 13, 2008

    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?

  • Geertjan Thursday, November 13, 2008

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

  • Tom Doan Saturday, October 2, 2010

    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://gw12345:password@my-mail-server.com).

    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.



  • guest Sunday, September 25, 2011

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

  • guest Tuesday, November 22, 2011

    I'm trying with gmail with this setting

    -L imap://gw12345:password@gmail.com

    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:

    java.net.ConnectException: Connection timed out: connect

    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665)

    at javax.mail.Service.connect(Service.java:295)

    at javax.mail.Service.connect(Service.java:176)

    at javax.mail.Service.connect(Service.java:125)

    at StoreTreeNode.loadChildren(StoreTreeNode.java:82)

    at StoreTreeNode.getChildCount(StoreTreeNode.java:73)

    at javax.swing.tree.DefaultTreeModel.getChildCount(DefaultTreeModel.java:168)

    ... 39 more

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.