Clay Banks
Clay Banks

Reputation: 4591

javax.naming.NameNotFoundException: Unable to find mail

I've setup a mail Resource definition in my context.xml

<Resource
  name="MyMailSession"
  auth="Container"
  type="javax.mail.Session"
  mail.smtp.sendpartial="true"
  mail.debug="true"
  mail.smtp.host="myhost.hosting.com"
/>

And a mail utility which looks up the mail session's JNDI:

 public int send() throws Exception {
        int msgCount = 0; //number of recipients 
        Context ctx = new InitialContext();
        Session session = (Session) ctx.lookup("java:comp/env/mail/MyMailSession");
        MimeMessage message = new MimeMessage(session);

But am getting the following exception:

javax.naming.NameNotFoundException: Name [mail/MyMailSession] is not bound in this Context. Unable to find [mail].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
    ...

Is the mail tag necessary as a prefix to the Resource name when it comes to mail or is it more of a convention? (i.e., looking up a User Transaction would be set as java:comp/UserTransaction)

Upvotes: 2

Views: 4356

Answers (1)

Jean-Fran&#231;ois Savard
Jean-Fran&#231;ois Savard

Reputation: 21004

Try with

<Resource
  name="mail/MyMailSession"
  auth="Container"
  type="javax.mail.Session"
  mail.smtp.sendpartial="true"
  mail.debug="true"
  mail.smtp.host="myhost.hosting.com"
/>

instead of

<Resource
  name="MyMailSession"
  auth="Container"
  type="javax.mail.Session"
  mail.smtp.sendpartial="true"
  mail.debug="true"
  mail.smtp.host="myhost.hosting.com"
/>

Edit : In that case it is necessary because your refer to the context as mail/MyMailSession in this line :

Session session = (Session) ctx.lookup("java:comp/env/mail/MyMailSession");

What follow java:comp/env/ must be your Resource name.

Upvotes: 2

Related Questions