The example Attachments.java, based on the code fragments in the sections Creating an AttachmentPart Object and Adding Content and Accessing an AttachmentPart Object, creates a message that has a text attachment and an image attachment. It then retrieves the contents of the attachments and prints the contents of the text attachment. You will find the code for the Attachments class in the following directory:
Attachments first creates a message in the usual way. It then creates an AttachmentPart for the text attachment:
AttachmentPart attachment1 = message.createAttachmentPart();
After it reads input from a file into a string named stringContent, it sets the content of the attachment to the value of the string and the type to text/plain and also sets a content ID.
attachment1.setContent(stringContent, "text/plain"); attachment1.setContentId("attached_text");
It then adds the attachment to the message:
The example uses a javax.activation.DataHandler object to hold a reference to the graphic that constitutes the second attachment. It creates this attachment using the form of the createAttachmentPart method that takes a DataHandler argument.
// Create attachment part for image URL url = new URL("file:///../xml-pic.jpg"); DataHandler dataHandler = new DataHandler(url); AttachmentPart attachment2 = message.createAttachmentPart(dataHandler); attachment2.setContentId("attached_image"); message.addAttachmentPart(attachment2);
The example then retrieves the attachments from the message. It displays the contentId and contentType attributes of each attachment and the contents of the text attachment.
The Attachments class takes a text file as an argument. You can specify any text file. The attachments directory contains a file named addr.txt that you can use.
To build the program using NetBeans IDE, follow these steps:
In NetBeans IDE, choose Open Project from the File menu.
In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/saaj/.
Select the attachments folder.
Select the Open as Main Project check box.
Click Open Project.
A Reference Problems dialog appears. Click Close.
Right-click the attachments project and choose Resolve Reference Problems.
In the Resolve Reference Problems dialog, select the first of the missing JAR files and click Resolve.
The missing files are activation.jar, javaee.jar, and appserv-ws.jar.
Navigate to the as-install/lib/ directory.
Select the missing JAR file (activation.jar, for example) and click Open.
In the Resolve Reference Problems dialog, all the files have green check marks to the left of their names.
Right-click the project and choose Build.
To run the program using NetBeans IDE, follow these steps:
Right-click the attachments project and choose Properties.
Select Run from the Categories tree.
In the Arguments field, type the name of a text file:
Right-click the project and choose Run.
To run Attachments using Ant, go to the directory tut-install/javaeetutorial5/examples/saaj/attachments/. Use the following command:
ant run-att -Dfile=path-name
Specify a text file as the path-name argument:
ant run-att -Dfile=addr.txt
When you run Attachments using this file, you will see output like the following:
Running Attachments. Attachment attached_text has content type text/plain Attachment contains: Update address for Sunny Skies, Inc., to 10 Upbeat Street Pleasant Grove, CA 95439 USA Attachment attached_image has content type image/jpeg