Commit a0bb55f0 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

9781565924796

0 → 120000
+1 −0
Changes for 9781565924796: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
9781565924796
 No newline at end of file

JBiff.java

0 → 100755
+187 −0
Changes for JBiff.java: 187 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * JBiff  : A simple program to notify you when new mail has arrived,
 *          specifically using the IMAP protocol.  Minimal error 
 *          checking is done to reduce code size.  
 *          To run this code, you will need mail.jar and activation.jar 
 *          from JavaMail and Java Activation Framework (JAF) respectively.  
 *          This code draws heavily on the "Monitoring a Mailbox"
 *          example in the JavaMail API v 1.1 (Appendix B, page 71)
 *
 * Author : Chris Ryan (cryan@plugged.net.au)
 *          Plugged In Software
 *
 * Copyright (c) 1999 Plugged In Software Pty Ltd.
 * Released under the GNU GPL - see http://www.gnu.org/copyleft/gpl.html
 *
 * Usage  : JBiff <username> <password> <host> <mailbox> <frequency, in seconds>
 *          e.g. java JBiff fnerk foobar mailin inbox 60
 *
 *          inbox refers to the username's default mailbox.  It's case
 *          insensitive. 
 */

import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class JBiff extends Frame {

  // Labels to print some info onto...
  public Label display_label;
  
  /*
   * Constructor.  Creates the various AWT components required
   * in the system.
   */
  public JBiff() {

    // create the simple GUI
    display_label = new Label("Please wait...",Label.LEFT);
    add(display_label);
    pack();
    setSize(600,30);
    display_label.setSize(600,30);
    setTitle("JBiff");
    setVisible(true);

    // create a handler to close the application
    addWindowListener(new WindowAdapter() {

      public void windowClosing(WindowEvent w) {

        // time to leave
        System.exit(0);

      }
 
    }); 

  }

  public final static void main(String[] args) throws Exception {

    // Check that the right number of parameters have been passed
    if (args.length != 5) {

      System.out.println("JBiff: Monitor the state of an IMAP mailbox.");
      System.out.println("usage: java JBiff <username> <password> <host> <mailbox> <frequency (sec)>");
      System.exit(1);

    }

    // Get the parameters from the command line.
    String username = args[0];
    String password = args[1];
    String host = args[2];
    String mailbox = args[3];  // use inbox for user's default/main box
    int frequency;
    try {

      frequency = Integer.parseInt(args[4]) * 1000;

    } catch (NumberFormatException nfe) {

      // bad number, just check every 5 mins.
      frequency = 300000;

      System.err.println("Your frequency value was bogus.  Using 5 minutes instead");

    }

    // create the gui system
    JBiff jbiff = new JBiff();

    /*
     * Get the system properties.  A number of properties can be
     * set that are useful to the JavaMail system, including
     * mail.host.  If mail.host is not set, JavaMail will use
     * localhost for the default mail server.
     */
    Properties system_properties = System.getProperties();

    // get the default JavaMail Session instance
    Session session = Session.getDefaultInstance(system_properties,null); 

    // switch on the session debugging to see what's happening
    //session.setDebug(true);

    try {

      // get the Store object for IMAP
      Store store = session.getStore("imap");

      // connect to the specified host
      store.connect(host,username,password);

      // get the Folder for this mailbox
      Folder folder = store.getFolder(mailbox);

      // ensure we got an existing folder that holds messages
      // not a sub folder!
      if (folder == null || !folder.exists() || folder.getType() == Folder.HOLDS_FOLDERS) {

        System.err.println("Invalid folder, or folder does not exist, or folder holds other folders!");
        System.exit(1);

      }

      // loop forever until the user kills us
      while (true) {

        /*
         * While the operations below can be done on a closed folder,
         * it's best to have the folder open, since each operation
         * would open & close the folder itself - not efficient!
         *
         * Another option would be to open the folder and register
         * a listener for a MessageCountEvent, however this would
         * require the folder to be held continually open in
         * READ_WRITE mode.  This could conflict with another
         * program trying to access the folder, e.g. a mail reader.
         */
        folder.open(Folder.READ_ONLY);

        // Get the current total number of messages
        int total_nr_messages = folder.getMessageCount();

        // Get the current number of unread messages
        int unread_nr_messages = folder.getUnreadMessageCount();

        // get the number of new messages
        int new_nr_messages = folder.getNewMessageCount();

        // display a status message...
        jbiff.display_label.setText("You have "+new_nr_messages+" new messages, "+unread_nr_messages+" unread messages and "+total_nr_messages+" total messages in your mailbox: "+mailbox);

        // close the folder, expunging messages is not our problem.
        folder.close(false);

        // try to sleep for a while
        try {

          Thread.sleep(frequency);

        } catch (InterruptedException ie) {

          // just ignore and continue on...

        }
        

      }

    } catch (MessagingException me) {

      System.err.println("Error: "+me.getMessage());
      System.exit(1);

    }

  }


}

MailOut.java

0 → 100755
+144 −0
Changes for MailOut.java: 144 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
 * MailOut: a simple program to email a message with an optional file
 *          attachment using the JavaMail API.  Minimal error checking
 *          is done to reduce code size.  To run this code, you will
 *          need mail.jar and activation.jar from JavaMail and Java
 *          Activation Framework (JAF) respectively.  
 *
 * Author : Chris Ryan (cryan@plugged.net.au)
 *          Plugged In Software
 *
 * Copyright (c) 1999 Plugged In Software
 * Released under GNU GPL - see http://www.gnu.org/copyleft/gpl.html
 *
 * Usage  : MailOut <from> <to> <subject> <message_text> [<filename>]
 *          e.g. java -Dmail.host=mailhost.somewhere.net MailOut me@somewhere.net you@elsewhere.com "Funny Jokes" "See the attached file for some funny jokes." jokes.txt
 * 
 */

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.*;
import java.util.*;

public class MailOut {

  public final static void main(String[] args) throws Exception {

    // Check that the right number of parameters have been passed
    if (!(args.length == 4 || args.length == 5)) {

      System.out.println("Usage: MailOut <from> <to> <subject> <message_text> [<filename>]");
      System.exit(1);

    }

    // Get the parameters from the command line.
    String from = args[0];
    String to = args[1];
    String subject = args[2];
    String message_text = args[3];
    String filename = null;

    // set the filename if we've got one.
    if (args.length == 5) {

      filename = args[4];

    }

    /*
     * Get the system properties.  A number of properties can be
     * set that are useful to the JavaMail system, including
     * mail.host.  If mail.host is not set, JavaMail will use
     * localhost for the default mail server.
     */
    Properties system_properties = System.getProperties();

    // get the default JavaMail Session instance
    Session session = Session.getDefaultInstance(system_properties,null); 

    // switch on the session debugging to see what's happening
    session.setDebug(true);

    try {

      // create a new MIME message
      Message message = new MimeMessage(session);

      /*
       * Create Internet style addresses for 'from' & 'to'.  The
       * assumption is that the default transport being used in
       * this session uses RFC822 addresses. 
       */
      InternetAddress from_internet_address = new InternetAddress(from);

      // assume there might be more than one recipient
      InternetAddress[] recipients = InternetAddress.parse(to);

      // set the 'from' in the message
      message.setFrom(from_internet_address);

      // set the recipient for the message - assume there might be more than one
      message.setRecipients(Message.RecipientType.TO,recipients);

      // set the subject for the message
      message.setSubject(subject);

      // do we have a file to send?
      if (filename != null) {

        // yes - create the individual parts of the MIME message

        // first, create the plain text message body
        MimeBodyPart message_body = new MimeBodyPart();
        message_body.setContent(message_text,"text/plain");

        /*
         * next, create the attachment body part.
         *
         * This requires using the Java Activation Framework to create
         * a DataHandler for the file.  DataHandler provides a consistent interface 
         * to data available in many different sources and formats.
         * In this case, we're using a local filename, so our DataSource for
         * the DataHandler is FileDataSource.  A DataSource provides a type
         * for the data (file) and access to it - I/O streams.  Refer to the
         * JAF API for more details.
         */
        DataSource file_data_source = new FileDataSource(filename);
        DataHandler file_data_handler = new DataHandler(file_data_source);
        MimeBodyPart file_attachment = new MimeBodyPart();
        file_attachment.setDataHandler(file_data_handler);

        // create the Multipart message
        Multipart multipart_message = new MimeMultipart();

        // add the two bodies to this multipart
        multipart_message.addBodyPart(message_body);
        multipart_message.addBodyPart(file_attachment);

        // set the content type of this message to indicate multipart
        message.setContent(multipart_message);

      } else {

        // no - send a plain message
        message.setContent(message_text,"text/plain");

      }

      // send the message using the default transport for this session
      Transport.send(message);

    } catch (MessagingException me) {

      System.err.println("Error sending message: "+me.getMessage());
      System.exit(1);

    }

  }


}

README.md

0 → 100644
+15 −0
Changes for README.md: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
## Example files for the title:  
	  
# Programming Internet Email, by David Wood
	  
[![Programming Internet Email, by David Wood](http://akamaicovers.oreilly.com/images/9781565924796/cat.gif)](https://www.safaribooksonline.com/library/view/title/9780596802585//)
	  
The following applies to example files from material published by O’Reilly Media, Inc. Content from other publishers may include different rules of usage. Please refer to any additional usage rights explained in the actual example files or refer to the publisher’s website.
	  
O'Reilly books are here to help you get your job done. In general, you may use the code in O'Reilly books in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from our books does not require permission. Answering a question by citing our books and quoting example code does not require permission. On the other hand, selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Incorporating a significant amount of example code from our books into your product's documentation does require permission.
	  
We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN.
	  
If you think your use of code examples falls outside fair use or the permission given here, feel free to contact us at <permissions@oreilly.com>.
	  
Please note that the examples are not production code and have not been carefully testing. They are provided "as-is" and come with no warranty of any kind.

logo.png

0 → 100644
+3.2 KiB
Loading image diff...
Loading