View Javadoc

1   package org.appfuse.webapp.services.impl;
2   
3   import org.apache.tapestry5.ioc.Messages;
4   import org.appfuse.model.User;
5   import org.appfuse.service.MailEngine;
6   import org.appfuse.webapp.services.EmailService;
7   import org.springframework.mail.MailException;
8   import org.springframework.mail.SimpleMailMessage;
9   import org.springframework.security.core.userdetails.UsernameNotFoundException;
10  
11  /**
12   *
13   * @author Serge Eby
14   */
15  public class EmailServiceImpl implements EmailService {
16      final private MailEngine mailEngine;
17      final private SimpleMailMessage simpleMailMessage;
18      final private Messages messages;
19  
20      public EmailServiceImpl(SimpleMailMessage simpleMailMessage, MailEngine mailEngine, Messages messages) {
21          this.simpleMailMessage = simpleMailMessage;
22          this.mailEngine = mailEngine;
23          this.messages = messages;
24      }
25  
26  
27      public void send(User user, String subject, String message, String url, boolean hint)
28              throws UsernameNotFoundException, MailException {
29  
30          // Message Template
31          StringBuilder msg = new StringBuilder(message);
32          // Skip credentials in Password Hint
33          if (!hint) {
34              msg.append("\n\n").append(messages.get("user.username"));
35              msg.append(": ").append(user.getUsername()).append("\n");
36              msg.append(messages.get("user.password")).append(": ");
37              msg.append(user.getPassword());
38          }
39          msg.append("\n\nLogin at: ").append(url);
40  
41          simpleMailMessage.setTo(user.getFullName() + "<" + user.getEmail() + ">");
42          simpleMailMessage.setSubject(subject);
43          simpleMailMessage.setText(msg.toString());
44  
45          mailEngine.send(simpleMailMessage);
46      }
47  }