001 package org.kuali.ole.deliver.batch; 002 003 import org.kuali.ole.deliver.loan.LoanProcessor; 004 import org.kuali.rice.core.api.config.property.ConfigContext; 005 import org.kuali.rice.core.api.mail.*; 006 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 007 import org.kuali.rice.core.mail.MailerImpl; 008 import org.springframework.mail.MailException; 009 import org.springframework.mail.SimpleMailMessage; 010 import org.springframework.mail.javamail.JavaMailSenderImpl; 011 012 import javax.activation.DataHandler; 013 import javax.mail.Address; 014 import javax.mail.Message; 015 import javax.mail.MessagingException; 016 import javax.mail.internet.AddressException; 017 import javax.mail.internet.InternetAddress; 018 import javax.mail.internet.MimeMessage; 019 import javax.mail.util.ByteArrayDataSource; 020 import java.io.IOException; 021 import java.util.Properties; 022 023 /** 024 * Created with IntelliJ IDEA. 025 * User: maheswarang 026 * Date: 4/23/13 027 * Time: 6:47 PM 028 * To change this template use File | Settings | File Templates. 029 */ 030 public class OleMailer extends MailerImpl{ 031 032 protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailer.class); 033 private static final String USERNAME_PROPERTY = "mail.smtp.username"; 034 private static final String PASSWORD_PROPERTY = "mail.smtp.password"; 035 private static final String HOST_PROPERTY = "mail.smtp.host"; 036 private static final String PORT_PROPERTY = "mail.smtp.port"; 037 private static final String MAIL_PREFIX = "mail"; 038 039 040 /** 041 * Send an email to a single recipient with the specified subject and message. This is a convenience 042 * method for simple message addressing. 043 * 044 * @param from 045 * sender of the message 046 * @param to 047 * list of addresses to which the message is sent 048 * @param subject 049 * subject of the message 050 * @param body 051 * 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 LOG.info("Mail Parameters :"+"Host : "+host+" Port :"+port+" User Name :"+userName+" Password"+password); 073 if(host!=null && !host.trim().isEmpty()){ 074 JavaMailSenderImpl.setHost(host); 075 }else if(host==null || (host!=null && host.trim().isEmpty())){ 076 host =properties.getProperty(HOST_PROPERTY); 077 JavaMailSenderImpl.setHost(host); 078 } 079 080 081 if(port!=null && !port.trim().isEmpty()){ 082 JavaMailSenderImpl.setPort(Integer.parseInt(port)); 083 } else if(port==null || (port!=null && port.trim().isEmpty())){ 084 port =properties.getProperty(PORT_PROPERTY); 085 if(port!=null){ 086 JavaMailSenderImpl.setPort(Integer.parseInt(port)); 087 } 088 } 089 090 091 092 if((userName!=null && !userName.trim().isEmpty()) || (password!=null && !password.trim().isEmpty())){ 093 JavaMailSenderImpl.setUsername(userName); 094 JavaMailSenderImpl.setPassword(password); 095 } else if((userName==null || (userName!=null && userName.trim().isEmpty())) || ((password==null || (password!=null && password.trim().isEmpty())))){ 096 userName =properties.getProperty(USERNAME_PROPERTY); 097 password =properties.getProperty(PASSWORD_PROPERTY); 098 JavaMailSenderImpl.setUsername(userName); 099 JavaMailSenderImpl.setPassword(password); 100 } 101 102 if (to.getToAddress() == null) { 103 LOG.warn("No To address specified. Refraining from sending mail."); 104 return; 105 } 106 try { 107 Address[] singleRecipient = {new InternetAddress(to.getToAddress())}; 108 super.sendMessage(from.getFromAddress(), 109 singleRecipient, 110 subject.getSubject(), 111 body.getBody(), 112 null, 113 null, 114 htmlMessage); 115 } catch (Exception e) { 116 LOG.error("Error occured while sending the mail : "+e.getMessage()); 117 //throw new RuntimeException(e); 118 } 119 } 120 121 122 123 124 125 }