Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:10:02+00:00 2026-05-27T08:10:02+00:00

I have a MailMessage class I’ve written to manage sending email from my application.

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:10:03+00:00Added an answer on May 27, 2026 at 8:10 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small email application that lets a user build a message from
I have a class that sends an Email (MailMessage) but I get the following
I have built a bulk email sending website for a client that is required
I'm trying to send an email message using the .NET MailMessage class which can
i have used System.Web.Mail.MailMessage to send HTML Formatted message to an email id. The
I have a simple application written that allows users to pick inventory items with
I have a simple ActionMailer class like this: class MyMailer < ActionMailer::Base def mail(from,
I have a function to download a mailmessage as MSG file from DocuShare server.
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.