1 25 package org.ofbiz.service.mail; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.IOException ; 29 import java.io.ByteArrayInputStream ; 30 import java.util.Properties ; 31 import javax.mail.MessagingException ; 32 import javax.mail.Session ; 33 import javax.mail.internet.MimeMessage ; 34 35 import org.ofbiz.base.util.Debug; 36 37 43 public class MimeMessageWrapper implements java.io.Serializable { 44 45 public static final String module = MimeMessageWrapper.class.getName(); 46 protected transient MimeMessage message = null; 47 protected transient Session session = null; 48 protected Properties mailProperties = null; 49 protected byte[] serializedBytes = null; 50 51 public MimeMessageWrapper(Session session, MimeMessage message) { 52 this(session); 53 this.setMessage(message); 54 } 55 56 public MimeMessageWrapper(Session session) { 57 this.setSession(session); 58 } 59 60 public void setSession(Session session) { 61 this.session = session; 62 this.mailProperties = session.getProperties(); 63 } 64 65 public synchronized Session getSession() { 66 if (session == null) { 67 session = Session.getInstance(mailProperties, null); 68 } 69 return session; 70 } 71 72 public void setMessage(MimeMessage message) { 73 if (message != null) { 74 this.message = message; 76 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 77 try { 78 message.writeTo(baos); 79 baos.flush(); 80 serializedBytes = baos.toByteArray(); 81 } catch (MessagingException e) { 82 Debug.logError(e, module); 83 } catch (IOException e) { 84 Debug.logError(e, module); 85 } finally { 86 try { 87 baos.close(); 88 } catch (IOException e) { 89 Debug.logError(e, module); 90 } 91 } 92 } 93 } 94 95 public synchronized MimeMessage getMessage() { 96 if (message == null) { 97 if (serializedBytes != null) { 99 ByteArrayInputStream bais = new ByteArrayInputStream (serializedBytes); 100 try { 101 message = new MimeMessage (this.getSession(), bais); 102 } catch (MessagingException e) { 103 Debug.logError(e, module); 104 } 105 } 106 } 107 return message; 108 } 109 } 110 | Popular Tags |