1 Reply Latest reply on Sep 23, 2010 4:04 AM by Shervin Asgari

    Sending Email failed issues

    sadique arslan Newbie
      i am trying to send email containing activation link for user who want to register my website. for this i have design following session and xhml pages. but when i request for it doesn't show and response regarding sending email. However my data is persisted in table successfully.

      please help me regarding this why email is unable to send to my client. if i need to change my code or to configure any settings.

      this is my session.

      package session.timi.dot.ca.gov;

      import entity.timi.dot.ca.gov.*;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.framework.EntityHome;

      import org.jboss.seam.international.StatusMessages;
      import org.jboss.seam.annotations.Transactional;
      import static org.jboss.seam.international.StatusMessage.Severity.INFO;
      import static org.jboss.seam.international.StatusMessage.Severity.ERROR;
      import entity.timi.dot.ca.gov.*;
      import org.jboss.seam.annotations.Logger;

      import java.io.IOException;
      import java.security.MessageDigest;
      import java.text.MessageFormat;
      import java.util.Map;
      import java.util.Set;
      import javax.faces.application.FacesMessage;
      import javax.faces.component.UIViewRoot;
      import javax.faces.context.ExternalContext;
      import javax.faces.context.FacesContext;
      import javax.persistence.PersistenceContext;
      import javax.persistence.EntityManager;
      import javax.persistence.Query;

      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotEmpty;
      import org.hibernate.validator.Pattern;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.core.Events;
      import org.jboss.seam.contexts.Contexts;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Transactional;
      import org.jboss.seam.international.StatusMessages;
      import static org.jboss.seam.international.StatusMessage.Severity.INFO;
      import static org.jboss.seam.international.StatusMessage.Severity.ERROR;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.security.Credentials;
      import org.jboss.seam.security.Identity;
      //import org.jboss.seam.ui.graphicImage.Image;
      import org.jboss.seam.util.Hex;
      import entity.timi.dot.ca.gov.*;




      @Name("authenticationInformationHome")
      public class AuthenticationInformationHome
                extends
                     EntityHome<AuthenticationInformation> {

          @Logger private Log log;
          @In(required = false) private PasswordSupport passwordSupport;
          @In(create=true) private StatusMessages statusMessages;
          private boolean agreedToTermsOfUse = false;

         
         
          @In(create = true)
           PersonHome personHome;
             
             
             
                     
           public void setAuthenticationInformationId(Integer id) {
                setId(id);
           }

           public Integer getAuthenticationInformationId() {
                return (Integer) getId();
           }

           @Override
           protected AuthenticationInformation createInstance() {
                AuthenticationInformation authenticationInformation = new AuthenticationInformation();
                return authenticationInformation;
           }

           public void load() {
                if (isIdDefined()) {
                     wire();
                }
           }

           public void wire() {
                getInstance();
                Person person = personHome.getDefinedInstance();
                if (person != null) {
                     getInstance().setPerson(person);
                }
           }

           public boolean isWired() {
                if (getInstance().getPerson() == null)
                     return false;
                return true;
           }

           public AuthenticationInformation getDefinedInstance() {
                return isIdDefined() ? getInstance() : null;
           }
             
            
             
              @Override
              public String persist()
              {
                
              // Her you put your code to add stuff to your instance. Remind you should add everything to the object returnded from getInstance(); !!
              // so : getInstance().setPassword(passwordSupport.getPassword());
              //      getInstance().setActivationKey(getMD5Hash(newUser.getLasts....
                  this.doRegister();
              return super.persist();
             }
             
             
             
             
          @Transactional
          public void doRegister() {

              if (!passwordSupport.isConfirmed()) {
                  statusMessages.addToControlFromResourceBundle("password", ERROR, "password_not_confirmed");
                  return;
              }
              if (!agreedToTermsOfUse) {
                  statusMessages.addToControlFromResourceBundle("termsOfUseLink", ERROR, "please_agree_to_terms");
                  return;
              }

              try {

                 // newUser.setFirstName(this.registrationScreenName);
                  getInstance().setActive(false);
                  //newUser.setPassword(passwordSupport.getPasswordHash(this.registrationScreenName));
                  getInstance().setPassword(passwordSupport.getPassword());
                  getInstance().setActivationKey(getMD5Hash(getInstance().getLastName()+getInstance().getCreateUserId()+getInstance().getEmail()+getInstance().getFirstName()+System.currentTimeMillis()));
                  getInstance().setCreatedOn(System.currentTimeMillis());
                 
      //           String s = "created" + " --" + newUser.getActivationKey() + " --" + newUser.getCreateUserId() + " " +  newUser.getEmail() + " " +  newUser.getPassword() + " " + newUser.getFirstName() + " " +  newUser.getLastName() + " " + newUser.getPerson().getId();
      //          statusMessages.addToControlFromResourceBundle("registerButton", ERROR, s);
                
             
                  // send the activation email ... as part of this transaction
      /*        
                  ExternalContext extCtxt = FacesContext.getCurrentInstance().getExternalContext();
                  String activationLink = ((javax.servlet.http.HttpServletRequest)extCtxt.getRequest()).getRequestURL().toString();
                  String newUserLink = activationLink+((activationLink.indexOf("?") != -1)?"&act=":"?act=")+ getInstance().getActivationKey();
                  Contexts.getEventContext().set("inactiveNewUser", new InactiveNewUser(getInstance().toString(),getInstance().getCreateUserId(),getInstance().getEmail(),newUserLink));
                  Events.instance().raiseEvent("userRegistered");
           
                  // do this last ... just to reset the register form ...
      */
              } catch (Exception exc) {
                  log.error("Registration failed for {0}", exc, String.valueOf(getInstance().getCreateUserId()));
                  // NOTE: we don't return the Exception message back to the newUser because it may reveal too much information to a hacker
                  statusMessages.addToControlFromResourceBundle("registerButton", ERROR, "general_reg_error");
                 
              }
            
           }
         
         
           public boolean getAgreedToTermsOfUse() {
              return agreedToTermsOfUse;
          }

          public void setAgreedToTermsOfUse(boolean agreedToTermsOfUse) {
              this.agreedToTermsOfUse = agreedToTermsOfUse;
          }

          protected String getMD5Hash(final String msg) {
              try {
                  MessageDigest md5 = MessageDigest.getInstance("MD5");
                  md5.reset();
                  return new String(Hex.encodeHex(md5.digest(msg.getBytes("UTF-8"))));
              } catch (Exception exc)
              {
                  throw new RuntimeException(exc);
              }
          }
         

      }


      this is my maile sender bean


      package session.timi.dot.ca.gov;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.AutoCreate;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Observer;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.faces.Renderer;

      @Name("registrationMailer")
      @AutoCreate
      @Scope(ScopeType.APPLICATION)
      public class RegistrationMailer {
          @In(create=true) private Renderer renderer;

          @Observer("userRegistered")
          public void sendActivationEmail() {
              renderer.render("/activation.xhtml");
          }

      }

      /////////
      package session.timi.dot.ca.gov;


      public class InactiveNewUser {

          private String displayName;
          private String userName;
          private String email;
          private String activationLink;

          public InactiveNewUser() {}

          public InactiveNewUser(String displayName, String userName, String email, String activationLink) {
              this.displayName = displayName;
              this.userName = userName;
              this.email = email;
              this.activationLink = activationLink;
          }

          public String getDisplayName() {
              return displayName;
          }

          public void setDisplayName(String displayName) {
              this.displayName = displayName;
          }

          public String getUserName() {
              return userName;
          }

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

          public String getEmail() {
              return email;
          }

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

          public String getActivationLink() {
              return activationLink;
          }

          public void setActivationLink(String activationLink) {
              this.activationLink = activationLink;
          }
      }

      //// this is file for email template

      <m:message xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:m="http://jboss.com/products/seam/mail"
                 xmlns:f="http://java.sun.com/jsf/core"
                 xmlns:h="http://java.sun.com/jsf/html"
                 xmlns:ui="http://java.sun.com/jsf/facelets"
                 xmlns:s="http://jboss.com/products/seam/taglib"
                 charset="UTF-8" importance="normal">
      <m:from name="JSF / RF Seam Example" address="support@saastk.com"/>
      <m:to name="#{inactiveNewUser.email}">#{inactiveNewUser.email}</m:to>
      <m:subject>JSF / RF Seam Example Account Activation</m:subject>
      <m:body>
      <html>
      <body>
      <p>Dear #{inactiveNewUser.displayName},</p>
      <p>Please click on the following link to activate your account:</p>
      <p><a href="#{inactiveNewUser.activationLink}">#{inactiveNewUser.activationLink}</a></p>
      </body>
      </html>
      </m:body>
      </m:message>