Tomcat 6 Javax.mail会话envCtx.lookup("mail/Session")不返回 [英] Tomcat 6 Javax.mail session envCtx.lookup("mail/Session") not returning

查看:124
本文介绍了Tomcat 6 Javax.mail会话envCtx.lookup("mail/Session")不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Tomcat 6通过Javax.mail API发送电子邮件客户端,我在server.xml中设置了我的配置,如下所示

I am using Tomcat 6 to send a email client with Javax.mail APIs , i set up my configurations in server.xml as below

<Resource name="mail/Session" auth="Container"
            type="javax.mail.Session"
            mail.smtp.host="localhost"/>

在下面的我的web.xml中

And in my web.xml as below

<resource-ref>
    <description>Resource reference to a container-managed JNDI JavaMail factory for sending e-mails.</description>
    <res-ref-name>mail/Session</res-ref-name>
    <res-type>javax.mail.Session</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

但是当我尝试使用context.lookup创建邮件会话时...

But when i tried to create a mail Session ...using context.lookup

ontext initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            *mailSession = (Session)envCtx.lookup("mail/Session");*

调用nvCtx.lookup("mail/Session");不返回....它会中断org/springframework/jms/listener/DefaultMessageListenerContainer ...

The call nvCtx.lookup("mail/Session"); is not returning....it goes and break in org/springframework/jms/listener/DefaultMessageListenerContainer ...

任何线索.... 感谢您的帮助.

Any Clues .... I appreciate any help.

谢谢 维杰

推荐答案

由于您不使用spring,请使用以下方法:

As you don't use spring use this:

来源: Apache Tomcat 6.0文档-JDNI资源

Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
Session session = (Session) envCtx.lookup("mail/Session");

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(request.getParameter("from")));
InternetAddress to[] = new InternetAddress[1];
to[0] = new InternetAddress(request.getParameter("to"));
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject(request.getParameter("subject"));
message.setContent(request.getParameter("content"), "text/plain");
Transport.send(message);

这篇关于Tomcat 6 Javax.mail会话envCtx.lookup("mail/Session")不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆