1 38 39 import java.util.*; 40 import javax.mail.*; 41 import javax.mail.internet.*; 42 import javax.mail.event.*; 43 import javax.activation.*; 44 45 64 public class transport implements ConnectionListener, TransportListener { 65 static String msgText = "This is a message body.\nHere's the second line."; 66 static String msgText2 = "\nThis was sent by transport.java demo program."; 67 68 public static void main(String [] args) { 69 Properties props = new Properties(); 70 InternetAddress[] addrs = null; 72 boolean debug = false; 73 if (args.length != 4) { 74 usage(); 75 return; 76 } else { 77 props.put("mail.smtp.user", args[1]); 78 props.put("mail.smtp.host", args[2]); 79 if (args[3].equals("true")) { 80 debug = true; 81 } else if (args[3].equals("false")) { 82 debug = false; 83 } else { 84 usage(); 85 return; 86 } 87 88 try { 90 addrs = InternetAddress.parse(args[0], false); 91 } catch (AddressException aex) { 92 System.out.println("Invalid Address"); 93 aex.printStackTrace(); 94 return; 95 } 96 } 97 Session session = Session.getInstance(props, null); 99 session.setDebug(debug); 100 101 transport t = new transport(); 102 t.go(session, addrs); 103 } 104 105 public transport() {} 106 107 public void go(Session session, InternetAddress[] toAddr) { 108 Transport trans = null; 109 110 try { 111 Message msg = new MimeMessage(session); 113 msg.setFrom(); 114 msg.setRecipients(Message.RecipientType.TO, toAddr); 115 msg.setSubject("JavaMail APIs transport.java Test"); 116 msg.setSentDate(new Date()); msg.setContent(msgText+msgText2, "text/plain"); 118 msg.saveChanges(); 119 120 trans = session.getTransport(toAddr[0]); 122 123 trans.addConnectionListener(this); 126 trans.addTransportListener(this); 127 128 trans.connect(); 130 131 trans.sendMessage(msg, toAddr); 133 134 try {Thread.sleep(5);}catch(InterruptedException e) {} 136 137 } catch (MessagingException mex) { 138 try {Thread.sleep(5);}catch(InterruptedException e) {} 140 141 mex.printStackTrace(); 142 System.out.println(); 143 Exception ex = mex; 144 do { 145 if (ex instanceof SendFailedException) { 146 SendFailedException sfex = (SendFailedException)ex; 147 Address[] invalid = sfex.getInvalidAddresses(); 148 if (invalid != null) { 149 System.out.println(" ** Invalid Addresses"); 150 if (invalid != null) { 151 for (int i = 0; i < invalid.length; i++) 152 System.out.println(" " + invalid[i]); 153 } 154 } 155 Address[] validUnsent = sfex.getValidUnsentAddresses(); 156 if (validUnsent != null) { 157 System.out.println(" ** ValidUnsent Addresses"); 158 if (validUnsent != null) { 159 for (int i = 0; i < validUnsent.length; i++) 160 System.out.println(" "+validUnsent[i]); 161 } 162 } 163 Address[] validSent = sfex.getValidSentAddresses(); 164 if (validSent != null) { 165 System.out.println(" ** ValidSent Addresses"); 166 if (validSent != null) { 167 for (int i = 0; i < validSent.length; i++) 168 System.out.println(" "+validSent[i]); 169 } 170 } 171 } 172 System.out.println(); 173 if (ex instanceof MessagingException) 174 ex = ((MessagingException)ex).getNextException(); 175 else 176 ex = null; 177 } while (ex != null); 178 } finally { 179 try { 180 trans.close(); 182 } catch (MessagingException mex) { } 183 } 184 } 185 186 public void opened(ConnectionEvent e) { 188 System.out.println(">>> ConnectionListener.opened()"); 189 } 190 public void disconnected(ConnectionEvent e) {} 191 public void closed(ConnectionEvent e) { 192 System.out.println(">>> ConnectionListener.closed()"); 193 } 194 195 public void messageDelivered(TransportEvent e) { 197 System.out.print(">>> TransportListener.messageDelivered()."); 198 System.out.println(" Valid Addresses:"); 199 Address[] valid = e.getValidSentAddresses(); 200 if (valid != null) { 201 for (int i = 0; i < valid.length; i++) 202 System.out.println(" " + valid[i]); 203 } 204 } 205 public void messageNotDelivered(TransportEvent e) { 206 System.out.print(">>> TransportListener.messageNotDelivered()."); 207 System.out.println(" Invalid Addresses:"); 208 Address[] invalid = e.getInvalidAddresses(); 209 if (invalid != null) { 210 for (int i = 0; i < invalid.length; i++) 211 System.out.println(" " + invalid[i]); 212 } 213 } 214 public void messagePartiallyDelivered(TransportEvent e) { 215 } 217 218 private static void usage() { 219 System.out.println("usage: java transport \"<to1>[, <to2>]*\" <from> <smtp> true|false\nexample: java transport \"joe@machine, jane\" senderaddr smtphost false"); 220 } 221 } 222 | Popular Tags |