Since I have written several articles on JavaMail for on developer.com (
WebMail in Java and
Java meets e-mail), I have received many requests for more information. This article
is a compilation of the most popular questions.
What is this mail server?
Internet mail requires clients, such as Outlook or Eudora, and servers, such as sendmail.
Clients are responsible for end-user interaction (preparing, replying, forwarding, reading
emails), the servers take of the actual delivery.
JavaMail is a client-only API. It lets your application interact with the mail server but it
does not replace it. The mail server does not use JavaMail.
How does JavaMail talk to the mail server?
By default, JavaMail uses the SMTP protocol and either POP3 or IMAP4.
SMTP, which stands for Simple Mail Transport Protocol, is used for outbound mail. In
other words, when your application has prepared a new email, JavaMail forwards it to
the server via SMTP. The server then forwards the mail to its recipients.
POP3 (Post Office Protocol, version 3) and IMAP4 (Internet Message Access Protocol,
version 4) are for inboud mail. Your application can access a mailbox (and retrieve the
mail) through either POP3 or IMAP4.
Which one should I use POP3 or IMAP4?
In most cases, you won’t have the choice. The mail administrator has installed one or the
other and that’s the one you will use.
POP3 is a simple protocol to download emails from a mailbox. IMAP4 is more
sophisticated and lets you sort emails in folder on the server. You will typically use
IMAP4 if more than one computer accesses the mailbox, since it is possible to store and
organize email on the server. In every other cases, POP3 is easier.
Do I need different servers for SMTP and POP3/IMAP4?
Strictly speaking, yes. JavaMail treats the outbound (SMTP) and inboud (POP3 or
IMAP4) servers as separate. However the mail administrator may have installed all the
packages on the same machine.
Can I use to any mail server?
For inbound mail, most likely yes. It is often desirable to access mailboxes from a remote
location, e.g. during business trips. This is also useful if you host the mailbox with an ISP.
For outbound mail, you will most likely have to use a local server. To fight against spam,
most servers are configured to forward local mails only.
JavaMail reports that the server refuses my mail!
The problem is probably not related to JavaMail but to the mail policy. Talk to your mail
administrator.
How to retrieve emails from my favourite free webmail?
Most free webmails offer POP3 access to the mailbox but they often charge a premium.
Look in the documentation for Outlook, Eudora or Netscape Mail support and copy the
POP3 configuration.
You cannot access a webmail through the web interface with JavaMail, you need POP3.
How to change the mailbox password through JavaMail?
You can’t.
Your server may offer a proprietary configuration API. Increasingly mail servers support
LDAP which is available to Java programmers through JNDI.
Can I add more protocols to JavaMail?
You can add new protocols through third parties plugins. Sun maintains a list of plugins
at http://java.sun.com/products/javamail/Third_Party.html.
At the time of writing, there are plugins for secure mail (S/MIME), local storage, NNTP
(usenet) and more.
Benoît Marchal is a Belgian developer and writer. He is the author of XML by Example (two editions), Applied XML Solutions, and Java Web Services. There’s more on this topic at marchal.com.