KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > OutgoingFolderInfo


1 package net.suberic.pooka;
2 import javax.mail.*;
3 import javax.mail.internet.*;
4
5 /**
6  * This class represents a folder which stores outgoing messages until they
7  * can connect to the SMTP server.
8  */

9 public class OutgoingFolderInfo extends FolderInfo {
10   URLName transportURL;
11   Thread JavaDoc sendMailThread = new net.suberic.util.thread.ActionThread(Pooka.getProperty("SendMailThread.name", "Send Mail Thread"));
12   boolean online = true;
13
14   /**
15    * Creates a new OutgoingFolderInfo for the given URLName.
16    */

17   public OutgoingFolderInfo(StoreInfo parent, String JavaDoc fname, URLName outgoingURL) {
18     super(parent, fname);
19     transportURL = outgoingURL;
20   }
21
22   /**
23    * Sends all available messages.
24    */

25   public void sendAll() throws javax.mail.MessagingException JavaDoc {
26     
27     Transport sendTransport = Pooka.getDefaultSession().getTransport(transportURL);
28     try {
29       sendTransport.connect();
30       
31       Message[] msgs = getFolder().getMessages();
32       
33       try {
34     for (int i = 0; i < msgs.length; i++) {
35       Message m = msgs[i];
36       if (! m.isSet(Flags.Flag.DRAFT)) {
37         sendTransport.sendMessage(m, m.getAllRecipients());
38         m.setFlag(Flags.Flag.DELETED, true);
39       }
40     }
41       } finally {
42     getFolder().expunge();
43       }
44     } finally {
45       sendTransport.close();
46     }
47   }
48
49   /**
50    * Virtually sends a message. If the current status is connected, then
51    * the message will actually be sent now. If not, and the
52    * Message.sendImmediately setting is true, then we'll attempt to send
53    * the message anyway.
54    */

55   public void sendMessage(NewMessageInfo nmi, boolean connect) throws javax.mail.MessagingException JavaDoc {
56     this.appendMessages(new MessageInfo[] { nmi });
57     if (online)
58       sendAll();
59     else if (connect || Pooka.getProperty("Message.sendImmediately", "false").equalsIgnoreCase("true")) {
60       try {
61     connect();
62     if (online)
63       sendAll();
64       } catch (MessagingException me) {
65     System.err.println("me is a " + me);
66     online = false;
67       }
68     }
69   }
70
71   /**
72    * Virtually sends a message. If the current status is connected, then
73    * the message will actually be sent now.
74    */

75   public void sendMessage(NewMessageInfo nmi) throws javax.mail.MessagingException JavaDoc {
76     sendMessage(nmi, false);
77   }
78
79   /**
80    * Attempts to connect this OutgoingFolderInfo.
81    */

82   public void connect() {
83     // does nothing right now.
84
online=true;
85   }
86 }
87
Popular Tags