Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials

jGuru: Fundamentals of the JavaMail API

 


[Help | API Docs | Short Course| Exercises]

In this exercise, create a program that sends a message with an attachment.

Prerequisites

Skeleton Code

Tasks

1. The skeleton code already includes the code to get the initial mail session.
2. From the session, get a Message and set its header fields: to, from, and subject.
3. Create a BodyPart for the main message cotent and fill its content with the text of the message.
4. Create a Multipart to combine the main content with the attachment. Add the main content to the multipart.
5. Create a second BodyPart for the attachment.
6. Get the attachment as a DataSource.
7. Set the DataHandler for the message part to the data source. Carry the original filename along.
8. Add the second part of the message to the multipart.
9. Set the content of the message to the multipart.
11. Compile and run the program, passing your SMTP server, from address, to address, and filename on the command line. This will send the file as an attachment.
12. Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...).

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

Executing the following command (replacing the mail server, from address, to address, and filename) sends a message with an attachment:

java AttachExample SMTP.Server from@address to@address filename

Next Exercise

Exercises

About This Short Course

Copyright 1996-2001 jGuru.com. All Rights Reserved.