Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Attachements in Java mail

 
Shiva Battula
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
trinity
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brain,
Thank you very much, Iam trying your solutions.
Best ragards,
Sivasankar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic