1 13 package org.ejbca.core.model.services.actions; 14 15 import java.util.Date ; 16 17 import javax.ejb.EJBException ; 18 import javax.mail.Message ; 19 import javax.mail.Session ; 20 import javax.mail.Transport ; 21 import javax.mail.internet.InternetAddress ; 22 import javax.mail.internet.MimeMessage ; 23 24 import org.apache.log4j.Logger; 25 import org.ejbca.core.model.InternalResources; 26 import org.ejbca.core.model.log.Admin; 27 import org.ejbca.core.model.log.LogEntry; 28 import org.ejbca.core.model.services.ActionException; 29 import org.ejbca.core.model.services.ActionInfo; 30 import org.ejbca.core.model.services.BaseAction; 31 32 39 public class MailAction extends BaseAction { 40 41 private static final Logger log = Logger.getLogger(MailAction.class); 42 43 private static final InternalResources intres = InternalResources.getInstance(); 44 45 private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER); 46 47 public static final String PROP_SENDERADDRESS = "action.mail.senderAddress"; 48 public static final String PROP_RECIEVERADDRESS = "action.mail.recieverAddress"; 49 50 57 public void performAction(ActionInfo actionInfo) throws ActionException { 58 checkConfig(actionInfo); 59 60 MailActionInfo mailActionInfo = (MailActionInfo) actionInfo; 61 String senderAddress = properties.getProperty(PROP_SENDERADDRESS); 62 63 String reciverAddress = mailActionInfo.getReciever(); 64 if(reciverAddress== null){ 65 reciverAddress = properties.getProperty(PROP_RECIEVERADDRESS); 66 } 67 68 if(reciverAddress == null || reciverAddress.trim().equals("")){ 69 String msg = intres.getLocalizedMessage("services.mailaction.errorreceiveraddress"); 70 throw new ActionException(msg); 71 } 72 73 try { 74 String mailJndi = getLocator().getString("java:comp/env/MailJNDIName"); 75 Session mailSession = getLocator().getMailSession(mailJndi); 76 77 Message msg = new MimeMessage (mailSession); 78 msg.setFrom(new InternetAddress (senderAddress)); 79 msg.addRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(reciverAddress, false)); 80 msg.setSubject(mailActionInfo.getSubject()); 81 msg.setContent(mailActionInfo.getMessage(), "text/plain"); 82 msg.setHeader("X-Mailer", "JavaMailer"); 83 msg.setSentDate(new Date ()); 84 Transport.send(msg); 85 86 String logmsg = intres.getLocalizedMessage("services.mailaction.sent", reciverAddress); 87 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_APPROVAL, new java.util.Date (), null, null, LogEntry.EVENT_INFO_NOTIFICATION, logmsg); 88 } catch (Exception e) { 89 String msg = intres.getLocalizedMessage("services.mailaction.errorsend", reciverAddress); 90 log.error(msg, e); 91 try{ 92 getLogSession().log(admin, admin.getCaId(), LogEntry.MODULE_APPROVAL, new java.util.Date (),null, null, LogEntry.EVENT_ERROR_NOTIFICATION, msg); 93 }catch(Exception f){ 94 throw new EJBException (f); 95 } 96 } 97 98 99 } 100 101 108 private void checkConfig(ActionInfo actionInfo) throws ActionException { 109 if(!(actionInfo instanceof MailActionInfo)){ 110 String msg = intres.getLocalizedMessage("services.mailaction.erroractioninfo"); 111 throw new ActionException(msg); 112 } 113 114 String senderAddress = properties.getProperty(PROP_SENDERADDRESS); 115 if(senderAddress == null || senderAddress.trim().equals("")){ 116 String msg = intres.getLocalizedMessage("services.mailaction.errorsenderaddress"); 117 throw new ActionException(msg); 118 } 119 } 120 121 122 123 } 124 | Popular Tags |