views:

1059

answers:

4

Using JDK1.5 how does one send a binary attachemnt (such as a PDF file) easily using the JavaMail API?

+1  A: 

If you want to do it easily I'd suggest using Commons-Email! It's built on the JavaMail API, but it makes it much simpler.

There is a sample in the User Guide on how to send email with attachments... it's much easier than using the standard JavaMail API!

Phill Sacre
+5  A: 

Have you looked at the JavaMail FAQ? It seems to have little snippets to demonstrate the process (and how to fix a common problem -- running out of memory).

+4  A: 

Assuming that you don't want to read some links and don't want any external dependencies, you need to use MimeMultipart and BodyPart:

MimeMultipart messageContent = new MimeMultipart();

BodyPart bodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(yourFile);
bodyPart.setDataHandler(new DataHandler(source);
bodyPart.setFileName("MyFile.ext");
bodyPart.setDisposition(Part.ATTACHMENT);

// Then add to your message:
messageContent.addBodyPart(bodyPart);

Attaching a body to the messages is just attaching a BodyPart with disposition Part.INLINE

cynicalman
A: 

it is not working fine