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

    Starting with the skeleton code, get the system Properties.

    Properties props = System.getProperties();
    

    Task 2

    Add the name of your SMTP server to the properties for the mail.smtp.host key.

    props.put("mail.smtp.host", host);
    

    Task 3

    Get a Session object based on the Properties.

    Session session = Session.getDefaultInstance(props, null);
    

    Task 4

    Create a MimeMessage from the session.

    MimeMessage message = new MimeMessage(session);
    

    Task 5

    Set the from field of the message.

    message.setFrom(new InternetAddress(from));
    

    Task 6

    Set the to field of the message.

    message.addRecipient(Message.RecipientType.TO, 
          new InternetAddress(to));
    

    Task 7

    Set the subject of the message.

    message.setSubject("Hello JavaMail");
    

    Task 8

    Set the content of the message.

    message.setText("Welcome to JavaMail");
    

    Task 9

    Use a Transport to send the message.

    Transport.send(message);
    

    Task 10

    Compile and run the program, passing your SMTP server, from address, and to address on the command line.

    java MailExample SMTP.Server from@address to@address
    

    Task 11

    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.