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);
message.setFrom(fromAddress);
message.setSubject(subject);

Multipart multiPart = new MimeMultipart();
BodyPart bodyContent = new MimeBodyPart();
bodyContent.setText(this.body);
multiPart.addBodyPart(bodyContent);


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

message.setContent(multiPart);

InternetAddress address = new InternetAddress(contact.getEmail().getAddress());
message.setRecipient(Message.RecipientType.TO, address);
Transport.send(message, message.getRecipients(Message.RecipientType.TO));
 
Paul Clapham
Sheriff
Posts: 21416
33
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