title: Accessing ManagedBean method from EJB
body: <p>I have an EJB that handles creation of customer user accounts that needs access to a managed bean (account scoped) which manages user verification keys for user accounts (the keys are transient, so they don't need to be handled by database calls). However, I can't figure out a way to send the verification key to the EJB (which generates the verification email that is send to a user).</p>
  
  <p><strong>AccountVerifierBean.java</strong></p>
  
  <pre><code>@ManagedBean(name = "accountVerifierBean", eager = true)
  @ApplicationScoped
  public class AccountVerifierBean implements Serializable {
      private HashMap&lt;String, String&gt; verificationKeyMapping;
  
      public AccountVerifierBean() {}
  
      public boolean verifyKey(String username, String key) {
          return key.equals(verificationKeyMapping.get(username));
      }
      public String generateKey(String username) {
          Date time = new Date();
          String key = username + time.toString();
          key = Encryption.hashSHA(key);
          verificationKeyMapping.put(username, key);
          return key;
      }
  }
  </code></pre>
  
  <p><strong>CustomerService.java</strong></p>
  
  <pre><code>@Named
  @Stateless
  @LocalBean
  public class CustomerService {
      @PersistenceContext(unitName = "MovieProject-ejbPU")
      private EntityManager em;
  
      private String username;
      private String password;
  
      //getters and setters
  
      public void validateEmail() {
          Properties serverConfig = new Properties();
          serverConfig.put("mail.smtp.host", "localhost");
          serverConfig.put("mail.smtp.auth", "true");
          serverConfig.put("mail.smtp.port", "25");
  
          try {
              Session session = Session.getInstance(serverConfig, new Authenticator() {
                  @Override
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication("&lt;ACCOUNT&gt;","&lt;PASSWORD&gt;");
                  }
              });
              MimeMessage message = new MimeMessage( session );
              message.setFrom(new InternetAddress("accounts@minimalcomputers.com","VideoPile"));
              message.addRecipient(
                  Message.RecipientType.TO, new InternetAddress(username)
              );
              message.setSubject("Welcome to VideoPile!");
              message.setContent("&lt;p&gt;Welcome to VideoPile, Please verify your email.&lt;/p&gt;&lt;p&gt;" + verifierKey + "&lt;/p&gt;", "text/html; charset=utf-8"); //verifierKey is what I'm trying to get from AccountVerifierBean.
              Transport.send( message );
          }
          catch (MessagingException ex){
              Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
          }
          catch (Exception e) {
              Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, e);
          }
      }
  
      public String encrypt(String password) {
          try {
              return new     String(Base64.encode(MessageDigest.getInstance("SHA").digest(password.getBytes())));
          } catch (NoSuchAlgorithmException ex) {
              Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
              return null;
          }
      }
  }
  </code></pre>
  
  <p>I've tried @Inject, @ManagedProperty, using the Application map, and using the ELContext. Nothing seems to work.</p>
  
  <p><strong>EDIT:</strong>
  I think there is something wrong with the bean. Any methods called from bean don't seem to do anything (EL is resolved, no bean method calls though).</p>
  
  <p>I've tested the annotations that it uses (both are javax.faces.bean.*)</p>