1 19 package org.openharmonise.commons.net; 20 21 import java.io.*; 22 import java.util.logging.*; 23 import java.util.logging.Level ; 24 25 import javax.activation.*; 26 import javax.mail.Multipart ; 27 import javax.mail.internet.*; 28 29 30 38 public class Email extends Object { 39 40 43 public static final String CTYPE_TXTPLAIN = "text/plain"; 44 45 48 public static final String CTYPE_HTML = "text/html"; 49 50 53 protected static final String HEADER_IMPORTANCE = "Importance"; 54 55 58 public static final String IMPORTANCE_HIGH = "High"; 59 60 63 private static boolean m_bAttachment = false; 64 65 68 protected String m_sImportance = null; 69 70 73 protected String to_address; 74 75 78 protected String from_address; 79 80 83 protected String message_text; 84 85 88 protected String subject_text; 89 90 93 protected String message_contenttype = CTYPE_TXTPLAIN; 94 95 98 protected String m_sMailhost = ""; 99 100 103 protected File m_fAttachment = null; 104 105 108 private static final Logger m_logger = Logger.getLogger(Email.class.getName()); 109 110 120 public Email(String mailhost,String to_address, String from_address, String subject_text, 121 String message_text) { 122 this.to_address = to_address; 123 this.from_address = from_address; 124 this.subject_text = subject_text; 125 this.message_text = message_text; 126 m_sMailhost = mailhost; 127 } 128 129 139 public Email(String mailhost,String to_address, String from_address, String subject_text, 140 String message_text, File attachment) 141 throws Exception { 142 this.to_address = to_address; 143 this.from_address = from_address; 144 this.subject_text = subject_text; 145 this.message_text = message_text; 146 m_fAttachment = attachment; 147 m_bAttachment = true; 148 m_sMailhost = mailhost; 149 } 150 151 156 public boolean send() { 157 try { 158 java.util.Properties props = new java.util.Properties (); 160 String mail_host = m_sMailhost; 161 props.put("mail.smtp.host", mail_host); 162 163 javax.mail.Session session = javax.mail.Session.getInstance(props); 164 165 if (!m_bAttachment) { 166 javax.mail.Message msg = new javax.mail.internet.MimeMessage ( 167 session); 168 169 if (m_sImportance != null) { 170 msg.setHeader(HEADER_IMPORTANCE, m_sImportance); 171 } 172 173 msg.setFrom( 174 new javax.mail.internet.InternetAddress (from_address)); 175 176 msg.setRecipient(javax.mail.Message.RecipientType.TO, 177 new javax.mail.internet.InternetAddress ( 178 to_address)); 179 msg.setSubject(subject_text); 180 msg.setSentDate(new java.util.Date ()); 181 182 183 msg.setContent(message_text, message_contenttype); 184 185 javax.mail.Transport.send(msg); 186 } else { 187 MimeMessage msg = new MimeMessage(session); 189 190 if (m_sImportance != null) { 191 msg.setHeader(HEADER_IMPORTANCE, m_sImportance); 192 } 193 194 msg.setFrom( 195 new javax.mail.internet.InternetAddress (from_address)); 196 197 msg.setRecipient(javax.mail.Message.RecipientType.TO, 198 new javax.mail.internet.InternetAddress ( 199 to_address)); 200 201 202 msg.setSubject(subject_text); 204 205 MimeBodyPart messageBodyPart = new MimeBodyPart(); 208 209 210 messageBodyPart.setText(message_text); 213 214 Multipart multipart = new MimeMultipart(); 218 219 220 multipart.addBodyPart(messageBodyPart); 222 223 MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 226 227 FileDataSource source = new FileDataSource(this.m_fAttachment); 229 230 231 messageBodyPart2.setDataHandler(new DataHandler(source)); 233 234 235 messageBodyPart2.setFileName(source.getName()); 237 238 239 multipart.addBodyPart(messageBodyPart2); 241 242 243 msg.setContent(multipart); 247 248 249 javax.mail.Transport.send(msg); 251 } 252 } catch (Exception e) { 253 254 return false; 255 } 256 257 return true; 258 } 259 260 266 public String testSend() { 267 StringWriter sw = new StringWriter(); 268 PrintWriter err_out = new PrintWriter(sw); 269 270 try { 271 java.util.Properties props = new java.util.Properties (); 273 String mail_host = m_sMailhost; 274 err_out.println("Using mail_host:" + mail_host + "<br>"); 275 276 props.put("mail.smtp.host", mail_host); 277 278 javax.mail.Session session = javax.mail.Session.getDefaultInstance( 279 props, null); 280 err_out.println("created mail session:" + props + "<br>"); 281 282 try { 283 javax.mail.Message msg = new javax.mail.internet.MimeMessage ( 285 session); 286 err_out.println("created mail msg:" + msg + "<br>"); 287 288 msg.setFrom( 289 new javax.mail.internet.InternetAddress (from_address)); 290 err_out.println("setFrom:" + from_address + "<br>"); 291 292 msg.setRecipient(javax.mail.Message.RecipientType.TO, 293 new javax.mail.internet.InternetAddress ( 294 to_address)); 295 err_out.println("setRecipient:" + to_address + "<br>"); 296 297 msg.setSubject(subject_text); 298 err_out.println("setSubject:" + subject_text + "<br>"); 299 300 msg.setSentDate(new java.util.Date ()); 301 err_out.println("setSentDate." + "<br>"); 302 303 304 msg.setContent(message_text, message_contenttype); 305 306 err_out.println("setText:" + message_text + "<br>"); 307 err_out.println("setContent:" + message_contenttype + "<br>"); 308 309 javax.mail.Transport.send(msg); 310 err_out.println("sent mail." + "<br>"); 311 } catch (javax.mail.MessagingException mex) { 312 err_out.println("\n--Exception handling for mail" + "<br>"); 313 314 m_logger.log(Level.WARNING, mex.getLocalizedMessage(), mex); 315 316 Exception ex = mex; 317 318 do { 319 if (ex instanceof javax.mail.SendFailedException ) { 320 javax.mail.SendFailedException sfex = 321 (javax.mail.SendFailedException ) ex; 322 javax.mail.Address [] invalid = 323 sfex.getInvalidAddresses(); 324 325 if (invalid != null) { 326 err_out.println(" ** Invalid Addresses" + 327 "<br>"); 328 329 if (invalid != null) { 330 for (int i = 0; i < invalid.length; i++) 331 err_out.println(" " + invalid[i] + 332 "<br>"); 333 } 334 } 335 336 javax.mail.Address [] validUnsent = 337 sfex.getValidUnsentAddresses(); 338 339 if (validUnsent != null) { 340 err_out.println(" ** ValidUnsent Addresses" + 341 "<br>"); 342 343 if (validUnsent != null) { 344 for (int i = 0; i < validUnsent.length; i++) 345 err_out.println(" " + 346 validUnsent[i] + "<br>"); 347 } 348 } 349 350 javax.mail.Address [] validSent = 351 sfex.getValidSentAddresses(); 352 353 if (validSent != null) { 354 err_out.println(" ** ValidSent Addresses" + 355 "<br>"); 356 357 if (validSent != null) { 358 for (int i = 0; i < validSent.length; i++) 359 err_out.println(" " + 360 validSent[i] + "<br>"); 361 } 362 } 363 } 364 365 err_out.println("<br>"); 366 } while ((ex = ((javax.mail.MessagingException ) ex).getNextException()) != null); 367 } 368 } 369 catch (Exception e) { 370 err_out.println("Exception sending mail:" + e.getMessage() + 371 "<br>"); 372 err_out.println(); 373 err_out.println("Stacktrace follows:" + "<br>"); 374 e.printStackTrace(err_out); 375 } 376 finally { 377 if (err_out != null) { 378 err_out.close(); 379 } 380 } 381 382 return sw.toString(); 383 } 384 385 391 public void SetEmailAddress(String new_email_address) { 392 this.to_address = new_email_address; 393 394 return; 395 } 396 397 400 public void SetContentType(String content_type) { 401 content_type = content_type.trim(); 402 403 if (content_type.equals(Email.CTYPE_HTML) || 404 content_type.equals(Email.CTYPE_TXTPLAIN)) { 405 this.message_contenttype = content_type; 406 } else { 407 throw new RuntimeException ("Cannot set content type to:" + 408 content_type + 409 ", allowed values are:" + CTYPE_HTML + 410 " " + CTYPE_TXTPLAIN); 411 } 412 } 413 414 419 public void setImportance(String sImportance) { 420 m_sImportance = sImportance; 421 } 422 } | Popular Tags |