1 2 7 8 package com.quikj.server.framework; 9 10 import java.util.*; 11 import javax.mail.*; 12 import javax.mail.internet.*; 13 import java.io.*; 14 15 16 20 public class AceMailMessage implements Serializable 21 { 22 23 24 public AceMailMessage() 25 { 26 } 27 28 public javax.mail.Message toEmail(javax.mail.Session session) 29 { 32 MimeMessage emsg = new MimeMessage(session); 33 34 String field = "\"from\" = " + from; 35 if (from != null) 36 { 37 if (addressValid(from) == true) 38 { 39 try 40 { 41 emsg.setFrom(new InternetAddress(from)); 42 } 43 catch (Exception ex) 44 { } 46 } 47 } 48 49 int size = to.size(); 50 for (int i = 0; i < size; i++) 51 { 52 String value = (String )to.elementAt(i); 53 field = "\"to\" = " + value; 54 if (addressValid(value) == false) 55 { 56 continue; 57 } 58 59 try 60 { 61 emsg.addRecipient(Message.RecipientType.TO, new InternetAddress(value)); 62 } 63 catch (Exception ex) 64 { } 66 } 67 68 size = cc.size(); 69 for (int i = 0; i < size; i++) 70 { 71 String value = (String )cc.elementAt(i); 72 field = "\"cc\" = " + value; 73 if (addressValid(value) == false) 74 { 75 continue; 76 } 77 78 try 79 { 80 emsg.addRecipient(Message.RecipientType.CC, new InternetAddress(value)); 81 } 82 catch (Exception ex) 83 { } 85 } 86 87 size = bcc.size(); 88 for (int i = 0; i < size; i++) 89 { 90 String value = (String )bcc.elementAt(i); 91 field = "\"bcc\" = " + value; 92 if (addressValid(value) == false) 93 { 94 continue; 95 } 96 97 try 98 { 99 emsg.addRecipient(Message.RecipientType.BCC, new InternetAddress(value)); 100 } 101 catch (Exception ex) 102 { } 104 } 105 106 field = "\"subject\" = " + subject; 107 if (subject != null) 108 { 109 try 110 { 111 emsg.setSubject(subject); 112 } 113 catch (Exception ex) 114 { 115 errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage(); 116 return null; 117 } 118 } 119 120 size = replyTo.size(); 121 if (size > 0) 122 { 123 field = "\"reply-to\""; 124 125 ArrayList reply_to = new ArrayList(); 126 for (int i = 0; i < size; i++) 127 { 128 String value = (String )replyTo.elementAt(i); 129 if (addressValid(value) == false) 130 { 131 continue; 132 } 133 134 try 135 { 136 reply_to.add(new InternetAddress(value)); 137 } 138 catch (Exception ex) 139 { } 141 } 142 143 if (reply_to.size() > 0) 144 { 145 try 146 { 147 InternetAddress[] temp = new InternetAddress[reply_to.size()]; 148 reply_to.toArray(temp); 149 emsg.setReplyTo(temp); 150 } 151 catch (Exception ex) 152 { 153 errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage(); 154 return null; 155 } 156 } 157 } 158 159 field = "\"body\" = " + body; 160 if (body != null) 161 { 162 try 163 { 164 emsg.setText(body); 165 } 166 catch (Exception ex) 167 { 168 errorMessage = "Error setting " + field + " : " + ex.getClass().getName() + " : " + ex.getMessage(); 169 return null; 170 } 171 } 172 else 173 { 174 errorMessage = "Invalid body : null"; 175 return null; 176 } 177 178 179 181 try 182 { 183 Address[] recipients = emsg.getAllRecipients(); 184 185 if (recipients == null || recipients.length == 0) 186 { 187 errorMessage = "No valid recipients"; 188 return null; 189 } 190 } 191 catch (Exception ex) 192 { 193 errorMessage = "Error getting recipients for validation : " + ex.getClass().getName() + " : " + ex.getMessage(); 194 return null; 195 } 196 197 198 return emsg; 199 } 200 201 public static boolean addressValid(String addr) 202 { 203 StringTokenizer tok = new StringTokenizer(addr, "@"); 205 if (tok.countTokens() < 2) 206 { 207 return false; 208 } 209 210 tok.nextToken(); 211 String domain = tok.nextToken(); 212 213 StringTokenizer tok2 = new StringTokenizer(domain, "."); 214 if (tok2.countTokens() < 2) 215 { 216 return false; 217 } 218 219 return true; 220 } 221 222 225 public java.util.Vector getBcc() 226 { 227 return bcc; 228 } 229 230 233 public void setBcc(java.util.Vector bcc) 234 { 235 this.bcc = bcc; 236 } 237 238 public void addBcc(String bcc) 239 { 240 (this.bcc).addElement(new String (bcc)); 241 } 242 243 public int numBcc() 244 { 245 return bcc.size(); 246 } 247 248 251 public java.lang.String getBody() 252 { 253 return body; 254 } 255 256 259 public void setBody(java.lang.String body) 260 { 261 this.body = body; 262 } 263 264 267 public java.util.Vector getCc() 268 { 269 return cc; 270 } 271 272 275 public void setCc(java.util.Vector cc) 276 { 277 this.cc = cc; 278 } 279 280 public void addCc(String cc) 281 { 282 (this.cc).addElement(new String (cc)); 283 } 284 285 public int numCc() 286 { 287 return cc.size(); 288 } 289 290 293 public java.lang.String getFrom() 294 { 295 return from; 296 } 297 298 301 public void setFrom(java.lang.String from) 302 { 303 this.from = from; 304 } 305 306 309 public java.lang.String getSubject() 310 { 311 return subject; 312 } 313 314 317 public void setSubject(java.lang.String subject) 318 { 319 this.subject = subject; 320 } 321 322 325 public java.util.Vector getTo() 326 { 327 return to; 328 } 329 330 333 public void setTo(java.util.Vector to) 334 { 335 this.to = to; 336 } 337 338 public void addTo(String to) 339 { 340 (this.to).addElement(new String (to)); 341 } 342 343 public int numTo() 344 { 345 return to.size(); 346 } 347 348 351 public java.util.Vector getReplyTo() 352 { 353 return replyTo; 354 } 355 356 359 public void setReplyTo(java.util.Vector replyTo) 360 { 361 this.replyTo = replyTo; 362 } 363 364 public void addReplyTo(String reply_to) 365 { 366 (this.replyTo).addElement(new String (reply_to)); 367 } 368 369 public int numReplyTo() 370 { 371 return replyTo.size(); 372 } 373 374 378 public java.lang.String getErrorMessage() 379 { 380 return errorMessage; 381 } 382 383 384 private String from = null; 385 private Vector to = new Vector(); 386 private Vector cc = new Vector(); 387 private Vector bcc = new Vector(); 388 private String subject = null; 389 private Vector replyTo = new Vector(); 390 private String body = null; 391 392 private String errorMessage = ""; 393 394 } 395 | Popular Tags |