|
Exploit JavaMail's SMTP and MBOX Support
Take a closer look at the JavaMail API's access to SMTP to receive mail and how to manage MBOX mailboxes
by Kevin Jones
Posted January 28, 2004
I introduced the JavaMail API in a previous article (see "JavaMail's Flexible Protocol Support," Java Pro Online, January 21, 2004) and explained how to use it to read mail from a POP3 server. In addition to receiving mail, JavaMail can also be used to send mail and to examine mailboxes, that is, e-mail that has been received and stored on the client. Here, let's look at how to send e-mail using Simple Mail Transfer Protocol (SMTP) and how to access MBOX mailboxes, a standard storage mechanism for many mail clients (see the sidebar, "Mail Protocols").
As you may imagine, SMTP access is a little different from POP3 access. You start the same way by accessing the Session, but then from there you create a Transport object and use that to connect to the server. Once you're connected you can create a message to send. The message contains the address you send to, the reply to address, the address of the sender (these may be different; for example, the reply to address may be the address of a list server), and the message itself. Different SMTP servers may work slightly differently; for example, SMTP servers today will typically stop relays, which means that the sender's or the receiver's address must be an address within the organization. Typically, this factor also means that the server will require authentication (see Listing 1). Notice that the mail.smtp.auth property has been set, which allows the application to authenticate to the server.
Now let's take a look at accessing messages that have already been stored on a client. While this example might seem unnecessary, it will also cover selecting a provider and processing a multipart message.
SMTP and POP3 access comes built into the JavaMail JDK; although, as mentioned previously they are not production quality. However, with MBOX we don't even have that luxury because different providers that do support the MBOX protocol have to be installed. Our code has to make sure that the appropriate provider is selected (the JavaMail specification defines how to install multiple providers). Luckily, each provider advertises the protocols it supports so the correct provider can be chosen:
Session session = Session.
getDefaultInstance(props);
Provider[] providers =
session.getProviders();
Provider provider = null;
for (int i = 0; i <
providers.length; i++)
{
Provider p = providers[i];
if(p.getProtocol().equals(
"mbox"))
provider = p;
}
You can also choose the provider based on the vendor and version information.
Once you have the provider, you then need to access the folder that contains the actual e-mails. Again there are various ways of doing this, and they all involve providing a URL to the folder. On my machine the Inbox folder is /home/kevinj/evolution/local/Inbox/mbox, and it can be accessed like this:
Store s = session.getStore(
provider);
Folder root = s.getFolder(
"/home/kevinj/evolution/local/
Inbox/mbox");
Once you have the folder you can then access the messages:
root.open(Folder.READ_ONLY);
Message[] messages =
root.getMessages();
for (int i = 0; i <
messages.length; i++)
{
// process messages
Message message = messages[i];
}
Back to top
|