JavaMail : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

JavaMail
Oracle’s generic Java API (Application Programming Interface) for mail. Oracle has a SMTP (Simple Mail Transfer Protocol), POP3 (Post Office Protocol version 3) and IMAP4 implementation you can version 1.5.6 Last revised/verified: 2016-08-10

JAF (Java Activation Framework) is now bundled with the JDK (Java Development Kit) . If you are using an older Java, to install JAF, open the download with WinZip and extract all files with folder names to the Program Files directory of some drive. Ditto for JavaMail. As of Java version 1.6, JAF is bundled with the JDK. Then make sure "J:\Program Files\jaf-1.1.1\activation.jar" and "J:\Program Files\javamail-1.5.6\javax.mail.jar" are explicitly mentioned in your classpath, (not just in a classpath directory!). Alternatively, add the jars to the ext directory.

You won’t likely directly use the JAF java.activation package, but JavaMail uses them. JavaMail also comes bundled with JavaEE.

Look on your local hard disk in the JAF downloaded docs J:\Program Files\jaf1.1.1\docs\javadocs\index.html or

JavaMail and JAF docs are not part of the JDK, but the source comes with EE (Enterprise Edition). JavaMail does not support the Windows proprietary MAPI (Messaging API) protocol. Oracle’s implementation of JavaMail is now open source.

JavaMail also supports Google Gmail and Yahoo mail.

The tutorial is the best way to get started, since it explains how all the classes fit together. The API docs give a lot of detail, but nothing on how it all fits together. Another way to understand it is to look at the demo examples, particularly msgsend.java to send and msgshow.java to receive. The term store refers to all your messages stored on the server’s hard disk. The term folder refers to the mail messages stored on the mail server in the SMTP INBOX. Folder. getMessages just gets message headers. MimeMessage. getContent gets the message body.

Installing JavaMail

  1. download
  2. Unzip
  3. Put a copy of javax.mail.jar in each of your ext directories, such as:
    ext directories :

Coding Tips

The names of protocols are case-sensitive and must be specified in lower case e.g. pop3, smtp and imap.

If you get a Message, you can’t close the Folder or Session until you are completely finished grabbing fields for the Message. Once the Session is gone, you can’t get any additional information about the Message. This is not that unusual, but it means you can’t write a method to return a Message object unless either:

  1. All the Message’s interesting fields have already been probed and cached.
  2. You keep the Session and Folder open. Until the caller is finished with the Message object.
  3. You can get a free account to experiment with IMAP (Internet Message Access Protocol) from FastMail.

Similarly you need to hold Folder and Session open in order to be able to later delete the Message from the server.

Most JavaMail applications involve reading a message and modifying it in some way and sending it on. Most mail servers will not permit you to modify the incoming message. You must create a new one and copy the parts over. There are three techniques you can use to copy.

  1. Part.getContent Part.setContent
  2. Part.getDataHandler Part.setDataHandler
  3. Multipart.getBodyPart Multipart.addBodyPart
The third method has the disadvantage you can’t modify the part after you copy. It has the advantages it has almost no overhead; you are just copying a pointer and it should work with any MIME (Multipurpose Internet Mail Extensions) type, even ones for which there is no DataHandler.

You probably want to use transport.sendMessage ( message, recipients ), not Transport. send( message );

Often

message.setFlag( Flags.Flag.SEEN, true );
message.setFlag( Flags.Flag.DELETED, true );
I have not yet tracked down why. I suspect it may be that the Session, Folder or MimeMessage times out if you wait too long.

JavaMail In Applets

JavaMail is not part of the standard distribution. You can’t then very well use it in Applets, since the browser would need to download the entire 307K JavaMail jar and 57K JAF jar every time the Applet ran. Better to use Java Web Start where the jars will be downloaded only once.

Awkward Characters

How to you talk JavaMail into using  RFC 2047 encoding to permit non-ASCII characters in the FROM: TO: and SUBJECT: fields. Message.setSubject does not let you specify an encoding. Neither is there a Message.setHeaderEncoding method. I guess you have to encode/decode it yourself using javax.mail.internet.MimeUtility.

JavaMail is does not provide a mailserver. You might use something like James rather that using your ISP (Internet Service Provider) ’s mailserver.

Learning More


I used JavaMail for my bulk emailer, to send the same email to many people.

This page is posted
on the web at:

http://mindprod.com/jgloss/javamail.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\javamail.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[44.222.149.13]
You are visitor number