This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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 Android Security Essentials Live Lessons 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
Author

Attachements in Java mail

Shiva Battula
Greenhorn

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,
Sivasankar


SCJP and SCWCD.
trinity
Greenhorn

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

com.oreilly.servlet.multipart.MultipartParser
com.oreilly.servlet.multipart.ParamPart
com.oreilly.servlet.multipart.FilePart

It is not very hard to implement your requirement.

regards,
Trinity
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"trinity",
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

thanks,
Dave
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");
anInlineBodyPart.setDisposition("INLINE");

aMimeMultipart.addBodyPart(anInlineBodyPart);

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

aMimeMultipart.addBodyPart(aMimeBodyPart);

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

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 java.io.*;
import javax.activation.*;

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

this.bb = 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
Greenhorn

Joined: Jun 06, 2005
Posts: 20
Hi Brain,
Thank you very much, Iam trying your solutions.
Best ragards,
Sivasankar
 
 
subject: Attachements in Java mail
 
Similar Threads
Finding email attachment size using java mail api
Difference between Java Mail API & Spring Mail
how to develop email website using j2ee
Need Open Source email sending application
Commons email