Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

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: 21416
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