1 21 22 27 28 package javax.mail; 29 30 import java.io.IOException ; 31 import java.net.*; 32 import java.util.Vector ; 33 import java.util.Hashtable ; 34 import java.util.Enumeration ; 35 import javax.mail.event.*; 36 37 54 55 public abstract class Transport extends Service { 56 57 63 public Transport(Session session, URLName urlname) { 64 super(session, urlname); 65 } 66 67 96 public static void send(Message msg) throws MessagingException { 97 msg.saveChanges(); send0(msg, msg.getAllRecipients()); 99 } 100 101 116 public static void send(Message msg, Address [] addresses) 117 throws MessagingException { 118 119 msg.saveChanges(); 120 send0(msg, addresses); 121 } 122 123 private static void send0(Message msg, Address [] addresses) 125 throws MessagingException { 126 127 if (addresses == null || addresses.length == 0) 128 throw new SendFailedException ("No recipient addresses"); 129 130 134 Hashtable protocols = new Hashtable (); 135 136 Vector invalid = new Vector (); 138 Vector validSent = new Vector (); 139 Vector validUnsent = new Vector (); 140 141 for (int i = 0; i < addresses.length; i++) { 142 if (protocols.containsKey(addresses[i].getType())) { 144 Vector v = (Vector )protocols.get(addresses[i].getType()); 145 v.addElement(addresses[i]); 146 } else { 147 Vector w = new Vector (); 149 w.addElement(addresses[i]); 150 protocols.put(addresses[i].getType(), w); 151 } 152 } 153 154 int dsize = protocols.size(); 155 if (dsize == 0) 156 throw new SendFailedException ("No recipient addresses"); 157 158 Session s = (msg.session != null) ? msg.session : 159 Session.getDefaultInstance(System.getProperties(), null); 160 Transport transport; 161 162 165 if (dsize == 1) { 166 transport = s.getTransport(addresses[0]); 167 try { 168 transport.connect(); 169 transport.sendMessage(msg, addresses); 170 } finally { 171 transport.close(); 172 } 173 return; 174 } 175 176 180 MessagingException chainedEx = null; 181 boolean sendFailed = false; 182 183 Enumeration e = protocols.elements(); 184 while (e.hasMoreElements()) { 185 Vector v = (Vector )e.nextElement(); 186 Address [] protaddresses = new Address [v.size()]; 187 v.copyInto(protaddresses); 188 189 if ((transport = s.getTransport(protaddresses[0])) == null) { 191 for (int j = 0; j < protaddresses.length; j++) 194 invalid.addElement(protaddresses[j]); 195 continue; 196 } 197 try { 198 transport.connect(); 199 transport.sendMessage(msg, protaddresses); 200 } catch (SendFailedException sex) { 201 sendFailed = true; 202 if (chainedEx == null) 204 chainedEx = sex; 205 else 206 chainedEx.setNextException(sex); 207 208 Address [] a = sex.getInvalidAddresses(); 210 if (a != null) 211 for (int j = 0; j < a.length; j++) 212 invalid.addElement(a[j]); 213 214 a = sex.getValidSentAddresses(); 216 if (a != null) 217 for (int k = 0; k < a.length; k++) 218 validSent.addElement(a[k]); 219 220 Address [] c = sex.getValidUnsentAddresses(); 222 if (c != null) 223 for (int l = 0; l < c.length; l++) 224 validUnsent.addElement(c[l]); 225 } catch (MessagingException mex) { 226 sendFailed = true; 227 if (chainedEx == null) 229 chainedEx = mex; 230 else 231 chainedEx.setNextException(mex); 232 } finally { 233 transport.close(); 234 } 235 } 236 237 if (sendFailed || invalid.size() != 0 || validUnsent.size() != 0) { 239 Address [] a = null, b = null, c = null; 240 241 if (validSent.size() > 0) { 243 a = new Address [validSent.size()]; 244 validSent.copyInto(a); 245 } 246 if (validUnsent.size() > 0) { 247 b = new Address [validUnsent.size()]; 248 validUnsent.copyInto(b); 249 } 250 if (invalid.size() > 0) { 251 c = new Address [invalid.size()]; 252 invalid.copyInto(c); 253 } 254 throw new SendFailedException ("Sending failed", chainedEx, 255 a, b, c); 256 } 257 } 258 259 279 public abstract void sendMessage(Message msg, Address [] addresses) 280 throws MessagingException ; 281 282 private Vector transportListeners = null; 284 285 294 public synchronized void addTransportListener(TransportListener l) { 295 if (transportListeners == null) 296 transportListeners = new Vector (); 297 transportListeners.addElement(l); 298 } 299 300 309 public synchronized void removeTransportListener(TransportListener l) { 310 if (transportListeners != null) 311 transportListeners.removeElement(l); 312 } 313 314 324 protected void notifyTransportListeners(int type, Address [] validSent, 325 Address [] validUnsent, 326 Address [] invalid, Message msg) { 327 if (transportListeners == null) 328 return; 329 330 TransportEvent e = new TransportEvent(this, type, validSent, 331 validUnsent, invalid, msg); 332 queueEvent(e, transportListeners); 333 } 334 } 335 | Popular Tags |