Glassfish 中未包含电子邮件附件
我编写了一个 MailMessage 类来管理从我的应用程序发送电子邮件。它对于纯文本消息工作得很好,当我在命令行上手动编译和运行它时,附件逻辑可以工作,但当我在 Glassfish 3.1 中运行它时,它无法包含我的附件。我假设一定存在一些微妙的类加载问题,我在命令行上设置了 CLASSPATH 环境,但我无法弄清楚需要更改哪些应用程序服务器设置。下面是我在命令行上运行时用来创建和发送邮件消息的代码:
public static void main(String[] args) throws Exception {
String[] toAddr = new String[] {"[email protected]"};
String subject = "This is a test";
String data = "This is a message body";
MailMessage mailMessage = new MailMessage(toAddr, subject, data);
mailMessage.addAttachment(new File("/etc/hosts"), "text/plain");
mailMessage.send();
}
如果我将此函数更改为由我的 servlet 调用的方法,并启用调试,则生成的邮件消息如下所示:
[#|2011-11-17T11:21:37.710-0500|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=105;_ThreadName=Thread-1;|Date: Thu, 17 Nov 2011 11:21:37 -0500 (EST)
From: [email protected]
To: [email protected]
Message-ID: <9116840.7.1321546897580.JavaMail...>
Subject: This is a test
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_6_16232037.1321546897569"
.
|#]
相比之下,当我运行命令行上的相同代码,它显示所有附件,每个附件都有单独的消息边界。这是我用来将附件添加到底层 MimeMessage 的函数:
private Multipart buildMultipartMessage(String messageBody)
throws MessagingException {
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(messageBody.toString());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
// Attach each of our files
for (File part : attachment.keySet()) {
BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(new FileDataSource(part)));
attachmentPart.setFileName(part.getName() + ".txt");
attachmentPart.setHeader("Content-Type", attachment.get(part));
attachmentPart.setHeader("Content-ID", part.getName());
attachmentPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(attachmentPart);
}
return multipart;
}
由我的 MailMessage 类调用和使用它:
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
InternetAddress[] addresses = new InternetAddress[mailTo.length];
for (int i = 0; i < mailTo.length; i++)
addresses[i] = new InternetAddress(mailTo[i]);
message.setRecipients(Message.RecipientType.TO, addresses);
message.setSubject(subject);
message.setSentDate(new Date());
Multipart multipart = buildMultipartMessage(messageBody.toString());
message.setContent(multipart);
同样,所有这些代码都按原样编译、运行并生成有效的电子邮件在命令行上运行时的附件。只有当我在 Glassfish 中执行同样的操作时,我才会收到一条空消息。
任何有关如何诊断此问题的建议将不胜感激。
Steve
更新:
如果我在调用 buildMultipartMessage() 之后、在 message.setContent(multipart) 之前添加此代码,我可以看到内容是正确的:
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/var/tmp/stf"));
multipart.writeTo(bos);
bos.close();
} catch (Exception ex) { }
/var/tmp/stf 文件包含带有附件的完整消息正文和分隔符。我仍然很困惑为什么它可以在命令行中工作,但不能在 Glassfish 中工作,但这些信息可能对解决问题有用。
I have a MailMessage class I've written to manage sending email from my application. It works fine for plain text messages, and the attachment logic works when I compile and run it manually on the command line, but it fails to include my attachments when I'm running it within Glassfish 3.1. I'm assuming there must be some subtle class loading problem that I'm clobbering on the command line by having my CLASSPATH environment set, but I haven't been able to figure out what application server setting I need to change. Here's the code I use to create and send my mail message when running on the command line:
public static void main(String[] args) throws Exception {
String[] toAddr = new String[] {"[email protected]"};
String subject = "This is a test";
String data = "This is a message body";
MailMessage mailMessage = new MailMessage(toAddr, subject, data);
mailMessage.addAttachment(new File("/etc/hosts"), "text/plain");
mailMessage.send();
}
If I change this function to a method called by my servlet, with debugging enabled, the resulting mail message looks like this:
[#|2011-11-17T11:21:37.710-0500|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=105;_ThreadName=Thread-1;|Date: Thu, 17 Nov 2011 11:21:37 -0500 (EST)
From: [email protected]
To: [email protected]
Message-ID: <9116840.7.1321546897580.JavaMail...>
Subject: This is a test
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_6_16232037.1321546897569"
.
|#]
By comparison, when I run the same code on the command line, it shows all of the attachments, each with the separate message boundary. This is the function I'm using to add the attachments to the underlying MimeMessage:
private Multipart buildMultipartMessage(String messageBody)
throws MessagingException {
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(messageBody.toString());
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
// Attach each of our files
for (File part : attachment.keySet()) {
BodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(new FileDataSource(part)));
attachmentPart.setFileName(part.getName() + ".txt");
attachmentPart.setHeader("Content-Type", attachment.get(part));
attachmentPart.setHeader("Content-ID", part.getName());
attachmentPart.setDisposition(Part.ATTACHMENT);
multipart.addBodyPart(attachmentPart);
}
return multipart;
}
Which is called and used like this by my MailMessage class:
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
InternetAddress[] addresses = new InternetAddress[mailTo.length];
for (int i = 0; i < mailTo.length; i++)
addresses[i] = new InternetAddress(mailTo[i]);
message.setRecipients(Message.RecipientType.TO, addresses);
message.setSubject(subject);
message.setSentDate(new Date());
Multipart multipart = buildMultipartMessage(messageBody.toString());
message.setContent(multipart);
Again, all of this code, exactly as is, compiles, runs, and produces a valid email with attachments when run on the command line. It's only when I do the same thing within Glassfish that I get an empty message.
Any suggestions on how to diagnose this would be most appreciated.
Steve
UPDATE:
If I add this code after the call to buildMultipartMessage(), but before message.setContent(multipart), I can see that the content is correct:
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/var/tmp/stf"));
multipart.writeTo(bos);
bos.close();
} catch (Exception ex) { }
The /var/tmp/stf file contains the full message body with attachments and separators. I'm still confused as to why this works from the command line, but not within Glassfish, but the information may be useful in resolving the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,我对这个问题的解决方案导致了当前的问题。在我的启动类路径中包含 javax.mail.jar ,在我认可的目录中包含activation.jar 是我可以使用记录器进行电子邮件处理的唯一方法,但它们无法正常使用。我不知道为什么会这样,但我发现消除 -Xbootclasspath 选项并从我认可的目录中删除activation.jar 可以解决问题。如果有人对原因有任何猜测,我很乐意做更多测试并报告。现在,我想我必须在没有电子邮件记录的情况下生活,因为附件是我的应用程序的要求。
也许如果我切换到 log4j 而不是使用本机 Java EE 6 日志记录,我就可以两全其美。
It turned out that my solution to this problem caused the current problem. Having javax.mail.jar in my boot classpath and activation.jar in my endorsed directory was the only way I could get email handling working with the logger, but then they didn't work for normal usage. I have no idea why this is, but I found that eliminating the -Xbootclasspath option and removing activation.jar from my endorsed directory fixed the problem. If anyone has any speculation as to why, I'd be glad to do some more testing and report back. For now, I guess I have to live without email logging, because the attachments are a requirement for my application.
Perhaps if I switch to log4j instead of using the native Java EE 6 logging, I can have the best of both worlds.