Page 2 of 6
Once you have a session, create a message. In this example, you're setting the message's from and to email addresses, the subject, and the body text, all taken originally from the command line. You're also setting some header information, including the date, and you can specify cc recipients if you want.
Finally, you send the message via the javax.mail.Transport class. If you wonder how it knows about our mail session, look back at the message's constructor.
The setText(...) convenience method in class javax.mail.Message (inherited from the javax.mail.Part interface) sets the message content to the supplied string and sets the MIME type to text/plain.
You're not limited to plain text, though: you can send other content types via the setDataHandler(...) method. In most cases you can take "other content types" to mean file attachments, such as Word documents, but for something
a bit more interesting, check out this code for sending a Java serialized object:
ByteArrayOutputStream byteStream=new ByteArrayOutputStream(); ObjectOutputStream objectStream=new ObjectOutputStream(byteStream); objectStream.writeObject(theObject); msg.setDataHandler(new DataHandler( new ByteArrayDataSource( byteStream.toByteArray(), "lotontech/javaobject" )));
You won't find the DataHandler class within the javax.mail.* package structure because it belongs to the JavaBeans Activation Framework (JAF) package javax.activation. Remember, you downloaded the JAF distribution as well as JavaMail. JAF provides a mechanism for handling typed data content, which for Internet content means MIME types.
And if you really do try the code above for sending a Java object by email, you'll have trouble locating the ByteArrayDataSource class, as neither mail.jar nor activation.jar include it. Try looking in the JavaMail demo directory!
As for those file attachments that you're more likely to be interested in initially, you would create a javax.activation.FileDataSource instance in the DataHandler's constructor. Of course, you're not likely to send a file alone; rather, it will probably be an attachment to a text message.
For that you need to understand the concept of multipart messages, so I'll introduce that concept now, in the context of receiving
email.
Earlier, I introduced the javax.mail.Part interface implemented by javax.mail.Message. I'll now explain its message parts, which are important in this example. To start, take a look at Figure 3.
Figure 3 shows a Message as created in the previous example that is both a message and message part, because it implements the Part interface. For any part, you can get its content (any Java object), and, in the case of a simple text message, the content
object may be a String. For a multipart message, the content will be of type Multipart, from which we can get hold of the individual body parts, which themselves implement the Part interface.

Figure 3. UML diagram for the mail.Part interface
In practice, all will become apparent as you step through the code for a SimpleReceiver class, which I'll present in three sections: first, the class definition and the main(...) method that takes connection details from the command line; second, the receive(...) method that captures and steps through the incoming messages; and finally, the printMessage(...) method that prints the header information and content of each message.
Well explainedBy Anonymous on May 12, 2009, 8:07 amThanks for the well explanation interest shown towards making viewers understanding the concept
Reply | Read entire comment
I'm getting this error when I try to run from cmd line. Someone By Anonymous on May 6, 2009, 6:56 pmMicrosoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\HP_Owner>workspace.MailApps.bin.com.gmail.mail.SimpleR eceiver...
Reply | Read entire comment
Vesijama Open Source JavaMail wrapper APIBy Anonymous on April 30, 2009, 10:51 amhttp://code.google.com/p/vesijama/ The Vesijama framework essentially is a wrapper around the JavaMail mailing API that allows users to define emails on a high...
Reply | Read entire comment
ThanksBy Anonymous on January 12, 2009, 7:17 amThank you for this precise page
Reply | Read entire comment
View all comments