1 package net.suberic.pooka; 2 import net.suberic.pooka.gui.*; 3 import net.suberic.util.thread.ActionThread; 4 import javax.mail.*; 5 import java.util.*; 6 7 14 public class MailQueue { 15 Hashtable transportTable; 16 Session session; 17 18 ActionThread thread = new ActionThread(Pooka.getProperty("thread.sendMailThread", "Send Mail Thread")); 19 20 public MailQueue(Session newSession) { 21 session = newSession; 22 transportTable = new Hashtable(); 23 thread.start(); 24 } 25 26 35 public void sendMessage(NewMessageInfo pNmi, URLName pTransportURL, String sendPrecommand) throws MessagingException { 36 48 49 final URLName transportURL = pTransportURL; 50 final NewMessageInfo nmi = pNmi; 51 final String precommand = sendPrecommand; 52 53 if (Pooka.getProperty("Message.sendImmediately", "false").equalsIgnoreCase("true")) { 54 thread.addToQueue(new net.suberic.util.thread.ActionWrapper(new javax.swing.AbstractAction () { 55 56 public void actionPerformed(java.awt.event.ActionEvent actionEvent) { 57 try { 58 Transport sendTransport; 59 if (precommand.length() > 0) { 60 try { 61 System.out.println("MailQueue: Running precommand: " + precommand); 62 Process p = Runtime.getRuntime().exec(precommand); 63 p.waitFor(); 64 } catch (Exception ex) 65 { 66 System.out.println("Could not run SMTP precommand:"); 67 ex.printStackTrace(); 68 } 69 } 70 System.out.println("MailQueue: Trying to connect.."); 71 sendTransport = session.getTransport(transportURL); 72 sendTransport.connect(); 73 74 Message m = nmi.getMessage(); 75 sendTransport.sendMessage(m, m.getAllRecipients()); 76 77 ((NewMessageProxy)nmi.getMessageProxy()).sendSucceeded(true); 78 } catch (MessagingException me) { 79 ((NewMessageProxy)nmi.getMessageProxy()).sendFailed(null,me); 80 } 81 } 82 }, thread), new java.awt.event.ActionEvent (nmi, 1, "message-send")); 83 84 85 } 86 87 } 88 89 101 public void sendQueued() throws MessagingException { 102 Enumeration keys = transportTable.keys(); 103 URLName transportURL; 104 Transport sendTransport; 105 Vector sendQueue; 106 Message msg; 107 boolean error = false; 108 109 while (keys.hasMoreElements()) { 110 transportURL = (URLName)keys.nextElement(); 111 sendQueue = (Vector)transportTable.get(transportURL); 112 113 if (!(sendQueue.isEmpty())) { 114 MessagingException returnException = null; 115 sendTransport = session.getTransport(transportURL); 116 sendTransport.connect(); 117 118 for (int i=sendQueue.size()-1 ; i >= 0; i--) { 119 msg = (Message)sendQueue.elementAt(i); 120 try { 121 sendTransport.sendMessage(msg, msg.getAllRecipients()); 122 sendQueue.removeElementAt(i); 123 } catch (SendFailedException sfe) { 124 if (returnException == null) 125 returnException = sfe; 126 else 127 returnException.setNextException(sfe); 128 } catch (MessagingException me) { 129 if (returnException == null) 130 returnException = me; 131 else 132 returnException.setNextException(me); 133 } 134 } 135 if (returnException != null) 136 throw returnException; 137 } 138 } 139 } 140 } 141 142 143 144 145 146 | Popular Tags |