This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Mail: Cant set email body text when including email attachment  RSS feed

david allen
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When I try and attach a file to an email it somehow removes the email body text.If I dont attach the file the body text appears. It must have something to do with how I am using the mMultipart object.

Below is my code. Could someone please show me what I am doing wrong?

// Define messgae
Message message = new MimeMessage(this.session);
Address fromAddress = new InternetAddress(fromEmail, fromName);

Multipart multiPart = new MimeMultipart();
BodyPart bodyContent = new MimeBodyPart();

// Add Attachment
if (file != null) {
BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new FileDataSource(file.getAbsolutePath())));


InternetAddress address = new InternetAddress(contact.getEmail().getAddress());
message.setRecipient(Message.RecipientType.TO, address);
Transport.send(message, message.getRecipients(Message.RecipientType.TO));
Paul Clapham
Posts: 22185
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you're doing wrong. That code looks functionally identical to code that I've had running in production for a couple of years with no problems.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!