Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials

jGuru: Fundamentals of the JavaMail API

 


[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.



    Task 1

    The skeleton code already includes the code to get the initial mail session.

    Task 2

    From the session, get a Message and set its header fields: to, from, and subject.

    Task 3

    Create a BodyPart for the main message cotent and fill its content with the text of the message.

    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText("Here's the file");
    

    Task 4

    Create a Multipart to combine the main content with the attachment. Add the main content to the multipart.

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);
    

    Task 5

    Create a second BodyPart for the attachment.

    Task 6

    Get the attachment as a DataSource.

    DataSource source = new FileDataSource(filename);
    

    Task 7

    Set the DataHandler for the message part to the data source. Carry the original filename along.

    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    

    Task 8

    Add the second part of the message to the multipart.

    Task 9

    Set the content of the message to the multipart.

    message.setContent(multipart);
    

    Task 10

    Send the message.

    Task 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.

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

    Task 12

    Check to make sure you received the message with your normal mail reader (Eudora, Outlook Express, pine, ...).

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