[英]impossible to send email in application deployed on Glassfish
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: 我花了整整两天的时间,用Google搜索它,但仍然无法解决它,让我们看看是否有人发现我在做错什么:
I've defined a javaMail Session in Glassfish, with all the parameters needed to communicate with a mail server (host, user, pass, port, etc..). 我在Glassfish中定义了一个javaMail会话,其中包含了与邮件服务器进行通信所需的所有参数(主机,用户,密码,端口等)。 I deploy an application that looks up that Session using jndi and tries to send an email. 我部署了一个使用jndi查找该Session并尝试发送电子邮件的应用程序。 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("zzz@yyy.com", "zzz"));
msg.setFrom(new InternetAddress("jjjj@kkkk.com", "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 现在,如果我部署没有mail.jar依赖项的应用程序,则在加载包含此代码的类时,我会得到一个
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 如果我将mail.jar放到domain / lib文件夹中,或者只是将一个类路径条目添加到我的应用程序清单中,包括mail.jar的位置,则不再具有ClassNotFoundException
,但是当执行查找并检索Session时,我得到了最古怪的
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. 这意味着我有两个版本的mail.jar,但是正如我所说的,如果我从/ lib中删除它或不将classpath条目添加到清单中,它甚至找不到一个。
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). 我在使用Glassfish 3.1.1和java-6-sun-1.6.0.26的 Ubuntu 11.10上(因此,activation.jar不是问题)。 Any idea will be much appreciated, as I can't think of anything else. 任何想法都将不胜感激,因为我别无其他。
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); JavaMail(所有javax.mail。*类)都包含在GlassFish中(假设您使用的是完整的Java EE平台版本,而不仅仅是Web Profile版本); you shouldn't need to include mail.jar in your application or install it manually. 您不需要在应用程序中包含mail.jar或手动安装它。 I can't explain why javax.mail.Message isn't found when you do that. 我无法解释为什么这样做时找不到javax.mail.Message。 The ClassCastException indicates that you have two versions of mail.jar available to the application. ClassCastException指示您对应用程序有两个版本的mail.jar。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.