2014-10-16 52 views
2

我設置郵件資源定義在我的context.xmljavax.naming.NameNotFoundException:無法找到郵件

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

和郵件工具,它會查找郵件會話的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); 

但我收到以下異常:

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

是作爲前綴的資源名稱所必需的郵件標籤,當談到郵寄還是更具約定? (即查找用戶事務將被設置爲的Java:比較/ UserTransaction的

回答

2

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

,而不是

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

編輯嘗試:在這種情況下,是必要的,因爲您在此行中將郵件/ MyMailSession稱爲上下文:

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

遵循java:comp/env /必須是您的資源名稱。

+0

我更想知道'mail'前綴是否必要,事實證明它不是。儘管如此,正確答案 – 2014-10-17 01:03:10