wood burning stoves 2.0*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail: Cant set email body text when including email attachment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail: Cant set email body text when including email attachment " Watch "Java Mail: Cant set email body text when including email attachment " New topic
Author

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

david allen
Ranch Hand

Joined: Sep 27, 2002
Posts: 185
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail: Cant set email body text when including email attachment
 
Similar Threads
JavaMail:Why can't a recipient using Lotus Notes see the attachment of my mail
Email problem when sending attachments
Attachment in email not working in server
JavaMail installation
javamail API question: How to send content type in message body when using multiparts