2

In Java SE its easy to support the authentication part with such code :

Session session = session.getInstance(props,new MyAuthenticator());

But in Java EE the session instance is not created by the application, but its supplied by JNDI injection by the application server in the application.

@Resource(name = "mail/JMsession")
private Session session

How to handle the authentication part here? What about the authenticationType argument in the @Resource annotation

@Resource(name = "mail/JMsession", authenticationType = AuthenticationType.APPLICATION)  
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
primeFaceUser
  • 295
  • 2
  • 15

2 Answers2

1

Normally in Java EE you should configure authentication in the same place as other properties for container-supplied sessions. For example for Glassfish this configuration can be found in the Admin UI / Resources / JavaMail Sessions. So, Authenticator is generally applicable only to SE-style:

Session session = session.getInstance(props,new MyAuthenticator());

Java EE configuration-based approach limits you to fixed number of preconfigured sessions. It, however, makes sense for most applications as they usually send mails via a fixed mail relay server.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Jk1
  • 11,233
  • 9
  • 54
  • 64
1

In Java EE you can use the @MailSessionDefinition annotation (or its equivalent in XML) to define the mail Session that you later on inject.

Its user and password attributes are the declarative version of the programmatic Authenticator's PasswordAuthentication.

@MailSessionDefinition is available on Java EE 7 servers, such as GlassFish 4 and also already in WildFly 8 (previously JBoss AS 8).

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • how can i use this annotation in my application, should i replace the session annotation with this annotation to support the authentication purpose ? – primeFaceUser Oct 30 '13 at 19:57
  • @primeFaceUser You should not replace anything. `@MailSessionDefinition` DEFINES the mail session, while `@Resource ... Session` is the point where you USE this mail session. It's defined once (put it on a random class) and potentially used at many locations. – Arjan Tijms Nov 01 '13 at 22:37