0

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[] {"steve.ferguson@epsilon.com"};
  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: sender@mydomain.com
To: steve.ferguson@mydomain.com
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("sender@mydomain.com"));
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.

Steve Ferguson
  • 792
  • 3
  • 10
  • 21

1 Answers1

0

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.

Community
  • 1
  • 1
Steve Ferguson
  • 792
  • 3
  • 10
  • 21