4 Replies Latest reply on Dec 21, 2008 3:13 PM by Jim Gallagher

    Issue sending email

    Jim Gallagher Newbie
      Hi

      I have a problem sending an email, described below:

      On my web application, I have a link for forgotten passwords.
      The user enters their userid
      My processor retrieves the name, email address and password from the database
      A bean in placed in conversation scope with these details
      I run Renderer.render on the email xhtml file, hoping to read the details from the bean
      It constantly fails trying to find the email address, which should come from the bean.

      My processor code:

      @Stateless
      @Name("forgottenpass")
      @Scope(ScopeType.CONVERSATION)
      public class ForgottenPasswordAction implements ForgottenPassword {

           @Logger
           private Log log;

           @In
           private EntityManager entityManager;

           @In(create = true)
           @Out
           private HelperPassword helperpassword;

           @In
           private PasswordRetrieval passwordretrieval;

           @In
           FacesMessages facesMessages;

           @In(create = true)
           private Renderer renderer;

           @End
           public String retrievePassword() {
                log.info("Retrieving password for " + passwordretrieval.getUsername());
                try {
                     helperpassword = (HelperPassword) entityManager.createQuery(
                               "from Helper where username = :username").setParameter(
                               "username", passwordretrieval.getUsername())
                               .getSingleResult();
                } catch (Exception e) {
                     e.printStackTrace();
                     facesMessages.add("Technical error - cannot send password!");
                     return null;
                }
                if (helperpassword.getEmail().equals(null)
                          || helperpassword.getEmail().equals("")) {
                     facesMessages
                               .add("Email address not valid - cannot send password!");
                     return null;
                }
                try {
                     System.out.println("Email address: " + helperpassword.getEmail());
                     renderer.render("/emails/passwordmessage.xhtml");
                     facesMessages.add("Email sent successfully");
                     return null;
                } catch (Exception e) {
                     e.printStackTrace();
                     facesMessages.add("Email sending failed: " + e.getMessage());
                     return null;
                }
           }
      }

      My bean code:

      @Entity
      @Name("helperpassword")
      @Scope(ScopeType.CONVERSATION)
      @Table(name = "helpers_personal")
      public class HelperPassword implements Serializable {

           /**
            *
            */
           @Transient
           private static final long serialVersionUID = 1L;

           @Id
           @GeneratedValue(strategy = GenerationType.AUTO)
           @Column(name = "ID")
           private int id;

           @Column(name = "Firstname")
           private String firstName;

           @Column(name = "SecondName")
           private String secondName;

           @Column(name = "Email")
           private String email;

           @Column(name = "username")
           private String userName;

           @Column(name = "password")
           private String password;

           public HelperPassword() {

           }

           public String getEmail() {
                return email;
           }

           public void setEmail(String email) {
                this.email = email;
           }

           public String getFirstName() {
                return firstName;
           }

           public void setFirstName(String firstName) {
                this.firstName = firstName;
           }

           public int getId() {
                return id;
           }

           public void setId(int id) {
                this.id = id;
           }

           public String getPassword() {
                return password;
           }

           public void setPassword(String password) {
                this.password = password;
           }

           public String getSecondName() {
                return secondName;
           }

           public void setSecondName(String secondName) {
                this.secondName = secondName;
           }

           public String getUserName() {
                return userName;
           }

           public void setUserName(String userName) {
                this.userName = userName;
           }

      }

      My email template:

      <m:message xmlns="http://www.w3.org/1999/xhtml"
           xmlns:m="http://jboss.com/products/seam/mail"
           xmlns:h="http://java.sun.com/jsf/html">
           <m:from name="OUTPUT ADDRESS" address="ADDRESS REMOVED" />
           <m:to name="Jim" address="#{helperpassword.email}" />
           <m:subject>Password Reminder</m:subject>
           <m:body type="plain">
                <p />
                <h:outputText value="Dear Jim" />,
                <p />You requested a password reminder. The password for your account is below:
                <p />Password: <h:outputText value="#{helperpassword.password}" />
                <p />Regards,
                <p />Fred
           </m:body>
      </m:message>