This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Attachements in Java mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Attachements in Java mail" Watch "Attachements in Java mail" New topic

Attachements in Java mail

Shiva Battula

Joined: Jun 06, 2005
Posts: 20
Hi All,
In my project,I need to implement sending email with more than one attachements.I know only how to send an email with Java mail API.
I dont know how to proceed with my requirement using Java mail API.
Can anyone help me out on this?
Thanks in advance,


Joined: Oct 15, 2005
Posts: 4
These classes could help you. Searching the net for few minutes, you can end up creating a beautiful architecture.


It is not very hard to implement your requirement.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Welcome back to the JavaRanch!

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with display names get deleted, often without warning

Brian Mozhdehi
Ranch Hand

Joined: Aug 17, 2006
Posts: 81
Its absoluetly not necessary to use any packages outside of the JavaMail API, I would not use anything from oreilly or anyone else. Why do it when JavaMail gives you what you need?

Example below....obviously needs to be rounded out

//create a MimeMultiPart
MimeMultipart aMimeMultipart = new MimeMultipart();

//add any inline data, i.e. "Hi Bob, here are some files"
MimeBodyPart anInlineBodyPart = new MimeBodyPart();
anInlineBodyPart.setContent(someString, "text/plain");


//loop thru and add your attachments
for(int ctr = 0; ctr < numberOfFiles; ctr++)
aFileBodyPart.setContent(someString, myContentType);


//set the content of the MimeMessage with your multipart
MimeMessage aMimeMessage = new MimeMessage(mySession);

The eone caveat is you may need to write a content handler for the data. If you dont want to do this, then you can use a DataSource to add a file directly. But the assumption I made above is that you have the raw data in memeory already. Writing data sources and content handlers is easy. Here is an example:

import javax.activation.*;

public class MyDataSource implements DataSource
public MyDataSource(byte[] bb, String contentType)
super(); = bb;
this.contentType = contentType;

public String getName()
return getClass().getName();

public String getContentType()
return contentType;

public InputStream getInputStream() throws IOException
return new ByteArrayInputStream(bb);

public OutputStream getOutputStream()
return null;

protected byte[] bb;
protected String contentType;

if you need more clarification, just ask!!!
Shiva Battula

Joined: Jun 06, 2005
Posts: 20
Hi Brain,
Thank you very much, Iam trying your solutions.
Best ragards,
I agree. Here's the link:
subject: Attachements in Java mail
It's not a secret anymore!