Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

MailTool.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 */
00021 
00022 import java.io.*;
00023 import java.sql.*;
00024 import java.util.*;
00025 import javax.mail.*;
00026 import javax.mail.event.*;
00027 import javax.mail.internet.*; 
00028 
00032 public class MailTool {
00033 
00034         private static Session session = null;
00035         private static Provider provider = null;
00036         private static Transport smtp = null;
00037         private static TransportListener listener = null;
00038 
00042         public synchronized static void init() throws Exception {
00043                 try {
00044                         if (session == null) session = Session.getDefaultInstance(new Properties());
00045                         if (provider == null) provider = session.getProvider("smtp");
00046                         if (smtp == null) smtp = session.getTransport("smtp");
00047                         if (listener == null) {
00048                                 smtp.addTransportListener(
00049                                         listener = new TransportListener() {
00050                                                 public void messageDelivered(TransportEvent e) {
00051                                                         System.out.println("messageDelivered: " + e);
00052                                                 }
00053                                                 public void messageNotDelivered(TransportEvent e) {
00054                                                         System.out.println("messageNotDelivered: " + e);
00055                                                 }
00056                                                 public void messagePartiallyDelivered(TransportEvent e) {
00057                                                         System.out.println("messagePartiallyDelivered: " + e);
00058                                                 }
00059                                         }
00060                                 );
00061                         }
00062                 }
00063                 catch (NoSuchProviderException e) {
00064                         throw(e); // rethrow for now
00065                 }
00066         }
00067 
00071         public static MailMessage newMessage() throws Exception {
00072                 init();
00073                 return new MailMessage(new MimeMessage(session));
00074         }
00075 
00079         public static void send (MailMessage message) throws Exception {
00080                 init();
00081                 if (message.message == null) throw new Exception("No MimeMessage!"); 
00082                 // TODO: implement a work queue to avoid this thread being blocked while
00083                 // the delivery is taking place.
00084                 smtp.send(message.message);
00085         }
00086 
00087 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15