How to get the session for mail flow in Java

Hi there,

I am needing the session in my Java mail class where I send the message. The problem is that this Session is not from jcrx … how can I get the session without the context compatible with the mail job?

How to get the session for mail flow in Java

Hi there,

I am needing the session in my Java mail class where I send the message. The problem is that this Session is not from jcrx … how can I get the session without the context compatible with the mail job?

thanks a lot

Hi,
A javax.mail.Session would typically be retrieved from JNDI and config in context.xml

If you have this in context.xml:

<Resource name="mail/Session"
          auth="Container"
          type="javax.mail.Session"
          mail.smtp.host="your.smtp.host (e.g. localhost)"/>

then you’d have something like this in code:

InitialContext initialContext = new javax.naming.InitialContext();
Context context = (Context) initialContext.lookup("java:comp/env");
Session session = (Session) context.lookup("mail/Session");

HTH
Jeroen

Hello,

I have problems with the Cast,

Job DEFAULT.76f12458-e743-4ff0-8200-252d89231803 threw an unhandle
d Exception:
[INFO] [talledLocalContainer] java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.jcr.Session


thnaks !

Yes of course, you’re mixing up javax.jcr.Session and javax.mail.Session!
You need to cast to same type you’ve set up in context.xml, i.e. the javax.mail one.

Jeroen

Hello jeroen, thanks for your answer.

He managed to implement the code, he sent the json payload to another server and when he tries to save on the destination server I get the following exception:

Caused by: org.onehippo.forge.content.exim.core.DocumentManagerException: Failed to add a document with ‘pruebita.1’ under ‘/content/documents/santander/santander-developer/en_gb/content/producercontainers/common-apis/apicontainers’.’. javax.jcr.RepositoryException: Unable to update item: item.save()
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentManagerImpl.createDocument(WorkflowDocumentManagerImpl.java:429) ~[content-exim-core-4.0.0.jar:?]
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentVariantImportTask.createDocument(WorkflowDocumentVariantImportTask.java:153) ~[content-exim-core-4.0.0.jar:?]
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentVariantImportTask.createOrUpdateDocumentFromVariantContentNode(WorkflowDocumentVariantImportTask.java:86) ~[content-exim-core-4.0.0.jar:?]
… 70 more
Caused by: javax.jcr.RepositoryException: Unable to update item: item.save()
at org.apache.jackrabbit.core.ItemSaveOperation.perform(ItemSaveOperation.java:265) ~[?:?]
at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216) ~[?:?]
at org.apache.jackrabbit.core.ItemImpl.perform(ItemImpl.java:91) ~[?:?]
at org.apache.jackrabbit.core.ItemImpl.save(ItemImpl.java:329) ~[?:?]
at org.apache.jackrabbit.core.session.SessionSaveOperation.perform(SessionSaveOperation.java:65) ~[?:?]
at org.apache.jackrabbit.core.session.SessionState.perform(SessionState.java:216) ~[?:?]
at org.apache.jackrabbit.core.SessionImpl.perform(SessionImpl.java:367) ~[?:?]
at org.apache.jackrabbit.core.SessionImpl.save(SessionImpl.java:856) ~[?:?]
at org.hippoecm.repository.impl.SessionDecorator.save(SessionDecorator.java:279) ~[?:?]
at org.hippoecm.repository.standardworkflow.FolderWorkflowImpl.add(FolderWorkflowImpl.java:441) ~[?:?]
at org.hippoecm.repository.standardworkflow.FolderWorkflowImpl.add(FolderWorkflowImpl.java:358) ~[?:?]
at org.hippoecm.repository.standardworkflow.FolderWorkflowImpl.add(FolderWorkflowImpl.java:351) ~[?:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_282]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_282]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_282]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_282]
at org.hippoecm.repository.impl.WorkflowManagerImpl$WorkflowInvocationHandler.invoke(WorkflowManagerImpl.java:382) ~[?:?]
at com.sun.proxy.$Proxy191.add(Unknown Source) ~[?:?]
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentManagerImpl.createDocument(WorkflowDocumentManagerImpl.java:418) ~[content-exim-core-4.0.0.jar:?]
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentVariantImportTask.createDocument(WorkflowDocumentVariantImportTask.java:153) ~[content-exim-core-4.0.0.jar:?]
at org.onehippo.forge.content.exim.core.impl.WorkflowDocumentVariantImportTask.createOrUpdateDocumentFromVariantContentNode(WorkflowDocumentVariantImportTask.java:86) ~[content-exim-core-4.0.0.jar:?]