无法在 Glassfish 上部署的应用程序中发送电子邮件

发布于 2024-12-29 05:45:46 字数 1482 浏览 1 评论 0原文

我实际上花了两天时间处理这个问题,到处用谷歌搜索,但仍然无法解决它,让我们看看是否有人发现我做错了什么:

我在 Glassfish 中定义了一个 javaMail 会话,其中包含与邮件服务器(主机、用户、通行证、端口等)。我部署了一个应用程序,该应用程序使用 jndi 查找该会话并尝试发送电子邮件。代码是:

InitialContext ctx = new InitialContext();
Session session = (Session) ctx.lookup("mail/javaMailSession");

Message msg = new MimeMessage(session);  
msg.setSubject("test");  
msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]", "zzz"));    
msg.setFrom(new InternetAddress("[email protected]", "hhh"));  
BodyPart messageBodyPart = new MimeBodyPart();  
messageBodyPart.setText("this is a test email");

Transport.send(msg);  

现在,如果我在没有 mail.jar 依赖项的情况下部署应用程序,则在加载包含此代码的类时,我会收到

ClassNotFoundException: javax.mail.Message,这意味着 mail.jar 不在类路径中。

如果我将 mail.jar 放入 domain/lib 文件夹中,或者只是将类路径条目添加到我的应用程序清单中(包括 mail.jar 的位置),我将不再遇到 ClassNotFoundException,但是当执行查找并检索会话,我得到最奇怪的

ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session.

这意味着我有两个版本的 mail.jar,但正如我所说,如果我从 /lib 中删除它或不将类路径条目添加到清单中,它甚至找不到一个。

我使用的是Ubuntu 11.10,使用Glassfish 3.1.1java-6-sun-1.6.0.26(所以activation.jar不是问题)。任何想法都会非常感激,因为我想不出其他的了。

I've spent literally two days with this, googled it everywhere and still couldnt solve it, let's see if someone finds what I'm doing wrong:

I've defined a javaMail Session in Glassfish, with all the parameters needed to communicate with a mail server (host, user, pass, port, etc..). I deploy an application that looks up that Session using jndi and tries to send an email. The code is:

InitialContext ctx = new InitialContext();
Session session = (Session) ctx.lookup("mail/javaMailSession");

Message msg = new MimeMessage(session);  
msg.setSubject("test");  
msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]", "zzz"));    
msg.setFrom(new InternetAddress("[email protected]", "hhh"));  
BodyPart messageBodyPart = new MimeBodyPart();  
messageBodyPart.setText("this is a test email");

Transport.send(msg);  

Now if I deploy my app without the mail.jar dependency, when the class that contains this code is loaded I get a

ClassNotFoundException: javax.mail.Message, meaning mail.jar is not in the classpath.

If I place mail.jar into domain/lib folder or just add a classpath entry into my app's manifest including the location of mail.jar, I no longer have the ClassNotFoundException, but when the lookup is executed and I retrieve the Session, I get the weirdest

ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session.

This would mean that I have two versions of mail.jar, but as I said if I remove it from /lib or dont add the classpath entry to the manifest it doesnt even find one.

I'm on Ubuntu 11.10, using Glassfish 3.1.1 and java-6-sun-1.6.0.26 (so activation.jar is not the problem). Any idea will be much appreciated, as I can't think of anything else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2025-01-05 05:45:46

JavaMail(所有 javax.mail.* 类)包含在 GlassFish 中(假设您使用的是完整的 Java EE 平台版本而不仅仅是 Web Profile 版本);您不需要在应用程序中包含 mail.jar 或手动安装它。我无法解释为什么当你这样做时找不到 javax.mail.Message 。 ClassCastException 表明您有两个版本的 mail.jar 可供应用程序使用。

JavaMail (all the javax.mail.* classes) is included in GlassFish (assuming you're using the full Java EE platform version and not just the Web Profile version); you shouldn't need to include mail.jar in your application or install it manually. I can't explain why javax.mail.Message isn't found when you do that. The ClassCastException indicates that you have two versions of mail.jar available to the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文