1 package Jt; 2 3 import java.util.*; 4 import java.io.*; 5 import javax.mail.*; 6 import javax.mail.internet.*; 7 import javax.activation.*; 8 9 12 13 public class JtMail extends JtObject { 14 private String from; 15 private String to; 16 private String message; 17 private String subject; 18 private String server; 19 private String cc; 20 private String attachment; 21 private String username; 22 private String password; 23 private Session ses = null; 24 25 26 void activate () { 27 28 if (server == null || from == null || to == null || 29 message == null) { 30 handleTrace ("JtMail: null attribute(s)"); 31 return; } 33 try { 34 Authenticator auth = null; 35 36 if (ses == null) { 37 if (username != null || password != null) 38 auth = new SMTPAuthenticator(); 39 Properties props = System.getProperties(); 41 props.put("mail.smtp.host", server); 43 if (username != null || password != null) 44 props.put("mail.smtp.auth", "true"); 45 46 47 ses = Session.getDefaultInstance(props, auth); 49 50 if (this.getObjTrace() == 1) 51 ses.setDebug(true); 52 } 53 54 56 MimeMessage msg = new MimeMessage(ses); 57 msg.setFrom(new InternetAddress(from)); 58 msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 59 if (cc != null && !cc.equals ("")) 60 msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc)); 61 62 msg.setSubject(subject); 63 if (attachment == null) 64 msg.setText(message); 65 else { 66 BodyPart messageBodyPart = new MimeBodyPart(); 67 messageBodyPart.setText(message); 69 Multipart multipart = new MimeMultipart(); 70 multipart.addBodyPart(messageBodyPart); messageBodyPart = new MimeBodyPart(); 72 DataSource source = new FileDataSource(attachment); 73 messageBodyPart.setDataHandler(new DataHandler(source)); 74 messageBodyPart.setFileName(attachment); 75 multipart.addBodyPart(messageBodyPart); 76 msg.setContent(multipart); 77 78 } 79 80 81 msg.setSentDate(new Date()); 83 84 85 Transport.send(msg); 87 } 88 catch (Exception e) { 89 handleException (e); 90 } 91 } 92 93 100 101 public Object processMessage (Object message) { 102 String content; 103 String query; 104 JtMessage e = (JtMessage) message; 105 106 107 if (e == null || (e.getMsgId() == null)) 108 return (null); 109 110 if (e.getMsgId().equals ("JtREMOVE")) { 112 return (null); 113 } 114 115 if (e.getMsgId().equals("JtACTIVATE")) { 117 activate (); 118 return (null); 119 } 120 121 handleError 122 ("processMessage: invalid message id:"+ 123 e.getMsgId()); 124 return (null); 125 } 126 127 130 131 public void setFrom (String newFrom) { 132 from = newFrom; 133 } 134 135 138 139 public String getFrom () { 140 return (from); 141 } 142 143 144 147 148 public void setTo (String newTo) { 149 to = newTo; 150 } 151 152 155 156 public String getTo () { 157 return (to); 158 } 159 160 163 164 public void setCc (String cc) { 165 this.cc = cc; 166 } 167 168 171 172 public String getCc () { 173 return (cc); 174 } 175 176 179 180 public void setMessage (String newMessage) { 181 message = newMessage; 182 } 183 184 185 188 189 public String getMessage () { 190 return (message); 191 } 192 193 194 197 198 public void setSubject (String newSubject) { 199 subject = newSubject; 200 } 201 202 203 206 207 public String getSubject () { 208 return (subject); 209 } 210 211 214 215 public void setUsername (String newUsername) { 216 username = newUsername; 217 } 218 219 220 223 224 public String getUsername () { 225 return (username); 226 } 227 228 231 232 public void setPassword (String newPassword) { 233 password = newPassword; 234 } 235 236 237 240 241 public String getPassword () { 242 return (password); 243 } 244 245 248 249 public void setServer (String newServer) { 250 server = newServer; 251 } 252 253 256 257 public String getServer () { 258 return (server); 259 } 260 261 264 265 public void setAttachment (String attachment) { 266 this.attachment = attachment; 267 } 268 269 270 273 274 public String getAttachment () { 275 return (attachment); 276 } 277 278 279 282 283 public static void main (String [] args) { 284 JtObject main; 285 JtMail jtmail; 286 287 main = new JtObject (); 288 main.createObject ("Jt.JtMessage", "message"); 290 291 293 jtmail = (JtMail) main.createObject ("Jt.JtMail", "jtmail"); 294 295 296 298 main.setValue ("jtmail", "from", "Jt@fsw.com"); 299 300 main.setValue ("jtmail", "subject", "Hello World!"); 301 main.setValue ("jtmail", "message", "Jt message"); 302 303 304 308 main.setValue ("jtmail", "to", "Jt@fsw.com"); 309 310 311 313 main.setValue ("message", "msgId", "JtACTIVATE"); 314 main.sendMessage ("jtmail", "message"); 315 316 317 319 if (jtmail.getObjException() == null) 320 System.out.println ("JtMail: GO"); 321 else 322 System.out.println ("JtMail: FAIL"); 323 324 325 328 329 331 main.removeObject ("jtmail"); 332 333 334 } 335 336 340 private class SMTPAuthenticator extends javax.mail.Authenticator 341 { 342 343 public PasswordAuthentication getPasswordAuthentication() 344 { 345 return new PasswordAuthentication(username, password); 348 } 349 } 350 } | Popular Tags |