1 26 27 package mailsb; 28 29 import java.rmi.RemoteException; 31 32 import javax.ejb.EJBException; 34 import javax.ejb.SessionBean; 35 import javax.ejb.SessionContext; 36 37 import javax.mail.Message; 38 import javax.mail.MessageContext; 39 import javax.mail.MessagingException; 40 import javax.mail.Transport; 41 import javax.mail.internet.MimePartDataSource; 42 import javax.naming.InitialContext; 43 import javax.naming.NamingException; 44 45 53 public class MimePartDSMailerBean implements SessionBean { 54 55 58 private String name = null; 59 60 64 private SessionContext sessionContext = null; 65 66 70 private Message message = null; 71 72 73 74 79 public void ejbCreate(String name) { 80 this.name = name; 81 } 82 83 84 85 101 public void setSessionContext(SessionContext sessionContext) throws EJBException, java.rmi.RemoteException { 102 this.sessionContext = sessionContext; 103 } 104 105 120 public void ejbRemove() throws EJBException, java.rmi.RemoteException { 121 } 123 124 139 public void ejbActivate() throws EJBException, java.rmi.RemoteException { 140 } 142 143 160 public void ejbPassivate() throws EJBException, java.rmi.RemoteException { 161 } 163 164 165 166 172 public void setMessage(String content) throws Exception, RemoteException { 173 174 InitialContext ictx = null; 176 try { 177 ictx = new InitialContext(); 178 } catch (NamingException e) { 179 throw new Exception("Can not get an inital context : " + e.getMessage()); 180 } 181 182 MimePartDataSource mimePartDataSource = null; 184 try { 185 mimePartDataSource = (MimePartDataSource) ictx.lookup("java:comp/env/mail/MailMimePartDataSource"); 186 } catch (NamingException e) { 187 throw new Exception("You have not configure the mail factory with the name specified" 188 + " in the jonas-ejb-jar.xml file for java:comp/env/mail/MailMimePartDataSource ." 189 + " By default, the factory's name is mailMimePartDS_1 :" + e.getMessage()); 190 } 191 192 MessageContext messageContext = mimePartDataSource.getMessageContext(); 194 195 if (messageContext == null) { 196 throw new Exception("Can not get the message Context of the mimepartDatasource"); 197 } 198 199 message = messageContext.getMessage(); 201 202 try { 203 message.setContent(content, "text/plain"); 204 } catch (MessagingException e) { 205 throw new Exception("A failure occurs when setting content of the message :" + e.getMessage()); 206 } 207 } 208 209 214 public void send() throws Exception, RemoteException { 215 216 if (message == null) { 217 throw new Exception("The message can not be send because the method setMessage() " 218 + " was not called before the send() method."); 219 } 220 221 try { 222 Transport.send(message); 223 } catch (MessagingException e) { 224 throw new Exception("The message can not be send : " + e.getMessage()); 225 } 226 } 227 228 } | Popular Tags |