View Javadoc
1   package org.kuali.ole.deliver.batch;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.deliver.processor.LoanProcessor;
5   import org.kuali.rice.core.api.config.property.ConfigContext;
6   import org.kuali.rice.core.api.mail.EmailBody;
7   import org.kuali.rice.core.api.mail.EmailFrom;
8   import org.kuali.rice.core.api.mail.EmailSubject;
9   import org.kuali.rice.core.api.mail.EmailTo;
10  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
11  import org.kuali.rice.core.mail.MailerImpl;
12  import org.springframework.core.io.FileSystemResource;
13  import org.springframework.mail.javamail.JavaMailSenderImpl;
14  import org.springframework.mail.javamail.MimeMessageHelper;
15  
16  import javax.activation.DataHandler;
17  import javax.activation.DataSource;
18  import javax.activation.FileDataSource;
19  import javax.mail.*;
20  import javax.mail.internet.*;
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.Date;
24  import java.util.List;
25  import java.util.Properties;
26  
27  /**
28   * Created with IntelliJ IDEA.
29   * User: maheswarang
30   * Date: 4/23/13
31   * Time: 6:47 PM
32   * To change this template use File | Settings | File Templates.
33   */
34  public class OleMailer extends MailerImpl {
35  
36      protected final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleMailer.class);
37      private static final String USERNAME_PROPERTY = "mail.smtp.username";
38      private static final String PASSWORD_PROPERTY = "mail.smtp.password";
39      private static final String HOST_PROPERTY = "mail.smtp.host";
40      private static final String PORT_PROPERTY = "mail.smtp.port";
41      private static final String MAIL_PREFIX = "mail";
42  
43  
44      /**
45       * Send an email to a single recipient with the specified subject and message. This is a convenience
46       * method for simple message addressing.
47       *
48       * @param from    sender of the message
49       * @param to      list of addresses to which the message is sent
50       * @param subject subject of the message
51       * @param body    body of the message
52       */
53      @Override
54      public void sendEmail(EmailFrom from, EmailTo to, EmailSubject subject, EmailBody body, boolean htmlMessage) {
55          JavaMailSenderImpl JavaMailSenderImpl = GlobalResourceLoader.getService("mailSender");
56          Properties properties = new Properties();
57          Properties configProps = ConfigContext.getCurrentContextConfig().getProperties();
58          LOG.debug("createInstance(): collecting mail properties.");
59          for (Object keyObj : configProps.keySet()) {
60              if (keyObj instanceof String) {
61                  String key = (String) keyObj;
62                  if (key.startsWith(MAIL_PREFIX)) {
63                      properties.put(key, configProps.get(key));
64                  }
65              }
66          }
67          LoanProcessor loanProcessor = new LoanProcessor();
68          String host = loanProcessor.getParameter(HOST_PROPERTY);
69          String port = loanProcessor.getParameter(PORT_PROPERTY);
70          String userName = loanProcessor.getParameter(USERNAME_PROPERTY);
71          String password = loanProcessor.getParameter(PASSWORD_PROPERTY);
72          if (LOG.isDebugEnabled()){
73              LOG.debug("Mail Parameters :" + "Host : " + host + " Port :" + port + " User Name :" + userName + " Password" + password);
74          }
75          if (host != null && !host.trim().isEmpty()) {
76              JavaMailSenderImpl.setHost(host);
77          } else if (host == null || (host != null && host.trim().isEmpty())) {
78              host = properties.getProperty(HOST_PROPERTY);
79              JavaMailSenderImpl.setHost(host);
80          }
81  
82  
83          if (port != null && !port.trim().isEmpty()) {
84              JavaMailSenderImpl.setPort(Integer.parseInt(port));
85          } else if (port == null || (port != null && port.trim().isEmpty())) {
86              port = properties.getProperty(PORT_PROPERTY);
87              if (port != null) {
88                  JavaMailSenderImpl.setPort(Integer.parseInt(port));
89              }
90          }
91  
92  
93          if ((userName != null && !userName.trim().isEmpty()) || (password != null && !password.trim().isEmpty())) {
94              JavaMailSenderImpl.setUsername(userName);
95              JavaMailSenderImpl.setPassword(password);
96          } else if ((userName == null || (userName != null && userName.trim().isEmpty())) || ((password == null || (password != null && password.trim().isEmpty())))) {
97              userName = properties.getProperty(USERNAME_PROPERTY);
98              password = properties.getProperty(PASSWORD_PROPERTY);
99              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, "true");
164         props.put(OLEConstants.SMTP_STARTTLS, "true");
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             Transport tr = session.getTransport(OLEConstants.TRANSPORT_PROTOCOL);
189             tr.connect(host, Integer.parseInt(port), userName, password);
190             tr.sendMessage(message, message.getAllRecipients());
191             LOG.debug("Mail Sent Successfully");
192             tr.close();
193         } catch (SendFailedException sfe) {
194             sfe.printStackTrace();
195         }
196     }
197 
198 }