문제

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)  
도움이 되었습니까?

해결책

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).

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top