001package org.kuali.ole.deliver.batch; 002 003import org.kuali.ole.OLEConstants; 004import org.kuali.ole.deliver.processor.LoanProcessor; 005import org.kuali.rice.core.api.config.property.ConfigContext; 006import org.kuali.rice.core.api.mail.EmailBody; 007import org.kuali.rice.core.api.mail.EmailFrom; 008import org.kuali.rice.core.api.mail.EmailSubject; 009import org.kuali.rice.core.api.mail.EmailTo; 010import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 011import org.kuali.rice.core.mail.MailerImpl; 012import org.springframework.core.io.FileSystemResource; 013import org.springframework.mail.javamail.JavaMailSenderImpl; 014import org.springframework.mail.javamail.MimeMessageHelper; 015 016import javax.activation.DataHandler; 017import javax.activation.DataSource; 018import javax.activation.FileDataSource; 019import javax.mail.*; 020import javax.mail.internet.*; 021import java.io.File; 022import java.io.IOException; 023import java.util.Date; 024import java.util.List; 025import java.util.Properties; 026 027/** 028 * Created with IntelliJ IDEA. 029 * User: maheswarang 030 * Date: 4/23/13 031 * Time: 6:47 PM 032 * To change this template use File | Settings | File Templates. 033 */ 034public class OleMailer extends MailerImpl { 035 036 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailer.class); 037 private static final String USERNAME_PROPERTY = "mail.smtp.username"; 038 private static final String PASSWORD_PROPERTY = "mail.smtp.password"; 039 private static final String HOST_PROPERTY = "mail.smtp.host"; 040 private static final String PORT_PROPERTY = "mail.smtp.port"; 041 private static final String MAIL_PREFIX = "mail"; 042 043 044 /** 045 * Send an email to a single recipient with the specified subject and message. This is a convenience 046 * method for simple message addressing. 047 * 048 * @param from sender of the message 049 * @param to list of addresses to which the message is sent 050 * @param subject subject of the message 051 * @param body body of the message 052 */ 053 @Override 054 public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) { 055 JavaMailSenderImpl JavaMailSenderImpl = GlobalResourceLoader.getService("mailSender"); 056 Properties properties = new Properties(); 057 Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); 058 LOG.debug("createInstance(): collecting mail properties."); 059 for (Object keyObj : configProps.keySet()) { 060 if (keyObj instanceof String) { 061 String key = (String) keyObj; 062 if (key.startsWith(MAIL_PREFIX)) { 063 properties.put(key, configProps.get(key)); 064 } 065 } 066 } 067 LoanProcessor loanProcessor = new LoanProcessor(); 068 String host = loanProcessor.getParameter(HOST_PROPERTY); 069 String port = loanProcessor.getParameter(PORT_PROPERTY); 070 String userName = loanProcessor.getParameter(USERNAME_PROPERTY); 071 String password = loanProcessor.getParameter(PASSWORD_PROPERTY); 072 if (LOG.isDebugEnabled()){ 073 LOG.debug("Mail Parameters :" + "Host : " + host + " Port :" + port + " User Name :" + userName + " Password" + password); 074 } 075 if (host != null && !host.trim().isEmpty()) { 076 JavaMailSenderImpl.setHost(host); 077 } else if (host == null || (host != null && host.trim().isEmpty())) { 078 host = properties.getProperty(HOST_PROPERTY); 079 JavaMailSenderImpl.setHost(host); 080 } 081 082 083 if (port != null && !port.trim().isEmpty()) { 084 JavaMailSenderImpl.setPort(Integer.parseInt(port)); 085 } else if (port == null || (port != null && port.trim().isEmpty())) { 086 port = properties.getProperty(PORT_PROPERTY); 087 if (port != null) { 088 JavaMailSenderImpl.setPort(Integer.parseInt(port)); 089 } 090 } 091 092 093 if ((userName != null && !userName.trim().isEmpty()) || (password != null && !password.trim().isEmpty())) { 094 JavaMailSenderImpl.setUsername(userName); 095 JavaMailSenderImpl.setPassword(password); 096 } else if ((userName == null || (userName != null && userName.trim().isEmpty())) || ((password == null || (password != null && password.trim().isEmpty())))) { 097 userName = properties.getProperty(USERNAME_PROPERTY); 098 password = properties.getProperty(PASSWORD_PROPERTY); 099 JavaMailSenderImpl.setUsername(userName); 100 JavaMailSenderImpl.setPassword(password); 101 } 102 103 if (to.getToAddress() == null) { 104 LOG.warn("No To address specified. Refraining from sending mail."); 105 return; 106 } 107 try { 108 Address[] singleRecipient = {new InternetAddress(to.getToAddress())}; 109 super.sendMessage(from.getFromAddress(), 110 singleRecipient, 111 subject.getSubject(), 112 body.getBody(), 113 null, 114 null, 115 htmlMessage); 116 } catch (Exception e) { 117 LOG.error("Exception occured while sending the mail : " + e.getMessage()); 118 //throw new RuntimeException(e); 119 } 120 } 121 122 123 public void SendEMail(String toAddress, String fromAddress,List fileNameList,String subject,String messageBody) throws MessagingException { 124 125 Properties properties = new Properties(); 126 Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); 127 LOG.debug("createInstance(): collecting mail properties."); 128 for (Object keyObj : configProps.keySet()) { 129 if (keyObj instanceof String) { 130 String key = (String) keyObj; 131 if (key.startsWith(MAIL_PREFIX)) { 132 properties.put(key, configProps.get(key)); 133 } 134 } 135 } 136 LoanProcessor loanProcessor = new LoanProcessor(); 137 String host = loanProcessor.getParameter(HOST_PROPERTY); 138 String port = loanProcessor.getParameter(PORT_PROPERTY); 139 String userName = loanProcessor.getParameter(USERNAME_PROPERTY); 140 String password = loanProcessor.getParameter(PASSWORD_PROPERTY); 141 if (LOG.isDebugEnabled()){ 142 LOG.debug("Mail Parameters :" + "Host : " + host + " Port :" + port + " User Name :" + userName + " Password" + password); 143 } 144 if (host != null && !host.trim().isEmpty()) { 145 host = host; 146 } else if (host == null || (host != null && host.trim().isEmpty())) { 147 host = properties.getProperty(HOST_PROPERTY); 148 } 149 if (port != null && !port.trim().isEmpty()) { 150 port = port; 151 } else if (port == null || (port != null && port.trim().isEmpty())) { 152 port = properties.getProperty(PORT_PROPERTY); 153 } 154 if ((userName != null && !userName.trim().isEmpty()) || (password != null && !password.trim().isEmpty())) { 155 userName = userName; 156 password = password; 157 } else if ((userName == null || (userName != null && userName.trim().isEmpty())) || ((password == null || (password != null && password.trim().isEmpty())))) { 158 userName = properties.getProperty(USERNAME_PROPERTY); 159 password = properties.getProperty(PASSWORD_PROPERTY); 160 } 161 Properties props = System.getProperties(); 162 props.put(OLEConstants.SMTP_HOST, host); 163 props.put(OLEConstants.SMTP_AUTH, properties.getProperty(OLEConstants.SMTP_AUTH)); 164 props.put(OLEConstants.SMTP_STARTTLS,properties.getProperty(OLEConstants.SMTP_STARTTLS)); 165 Session session = Session.getInstance(props, null); 166 167 MimeMessage message = new MimeMessage(session); 168 if (fromAddress != null) { 169 message.setFrom(new InternetAddress(fromAddress)); 170 } 171 if (toAddress != null) { 172 message.setRecipients(Message.RecipientType.TO, toAddress); 173 } 174 message.setSubject(subject); 175 BodyPart messageBodyPart = new MimeBodyPart(); 176 messageBodyPart.setText(messageBody); 177 Multipart multipart = new MimeMultipart(); 178 multipart.addBodyPart(messageBodyPart); 179 for (int i = 0; i < fileNameList.size(); i++) { 180 messageBodyPart = new MimeBodyPart(); 181 DataSource source = new FileDataSource((String) fileNameList.get(i)); 182 messageBodyPart.setDataHandler(new DataHandler(source)); 183 messageBodyPart.setFileName((String) fileNameList.get(i)); 184 multipart.addBodyPart(messageBodyPart); 185 message.setContent(multipart); 186 } 187 try { 188 String transferProtocol = properties.getProperty(OLEConstants.TRANSPORT_PROTOCOL); 189 Transport tr = session.getTransport(transferProtocol); 190 tr.connect(host, Integer.parseInt(port), userName, password); 191 tr.sendMessage(message, message.getAllRecipients()); 192 LOG.debug("Mail Sent Successfully"); 193 tr.close(); 194 } catch (SendFailedException sfe) { 195 sfe.printStackTrace(); 196 } 197 } 198 199}