how to : mail attached file store in binary format in database
how to attached mail with file that store in binary format in database ?
the code below is attached thru the browse button with the file path
because the attachment is store in binary format, how do i modify my code to get the mail attached with the attachement
File Name : [ C:\README.txt ] BROWSE
if (hasAttachment){
// avoid the case with no text parts
MimeBodyPart bodyPart =new MimeBodyPart();
bodyPart.setContent("Attachment enclosed.","text/plain");
mp.addBodyPart(bodyPart);
// the part with the file
FileDataSource fds =
new FileDataSource(bean.getAttachedFileName());
MimeBodyPart attachmentBodyPart =new MimeBodyPart();
DataHandler dh =new DataHandler(fds);
attachmentBodyPart.setDataHandler(dh);
attachmentBodyPart.setFileName(fds.getName());
mp.addBodyPart(attachmentBodyPart);
}
msg.setContent(mp);
[1261 byte] By [
yzmewona] at [2007-11-27 4:31:12]

# 1
If I understand your problem correctly, you are looking for a way to add an attachment read from a database.
One way would be to create a DatabaseDataSource that implements javax.activation.DataSource
if (hasAttachment) {
// avoid the case with no text parts
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent("Attachment enclosed.", "text/plain");
mp.addBodyPart(bodyPart);
// create a datasource that reads from a DataBase
DataSource ds = new DatabaseDataSource(bean.getAttachementId);
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataHandler dh = new DataHandler(ds);
attachmentBodyPart.setDataHandler(dh);
attachmentBodyPart.setFileName(ds.getName());
mp.addBodyPart(attachmentBodyPart);
}
msg.setContent(mp);
# 2
String id=bean.getAttachmentId();
DataSource ds=new DatabaseDataSource(id);
i have problem in this ....
import javax.sql.DataSource
or
import javax.activation.DataSource
still have error
2) if the attachment is store in binary format that is not in the database,
how to i do that ?
First, the file send from out from the standalone application that store in an object as byte[],
i will store in an object later pass to sendMail() method
bean.setAttachment(byteBuffer.toByteArray());
if i am store the file in byte[],
can i using this
DataSource ds=new ByteArrayDataSource(bs,"application/x-any");
sendMail(){
if (hasAttachment) {
// avoid the case with no text parts
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setContent("Attachment enclosed.", "text/plain");
mp.addBodyPart(bodyPart);
// the part with the file
DataSource fds =
new ByteArrayDataSource(bean.getAttachment(),"application/x-any");
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataHandler dh = new DataHandler(fds);
attachmentBodyPart.setDataHandler(dh);
attachmentBodyPart.setFileName(fds.getName());
mp.addBodyPart(attachmentBodyPart);
}
msg.setContent(mp);
Message was edited by:
yzmewon
Message was edited by:
yzmewon
# 3
> String id=bean.getAttachmentId();
> DataSource ds=new DatabaseDataSource(id);
>
> i have problem in this ....
>
> import javax.sql.DataSource
> or
> import javax.activation.DataSource
javax.activation.DataSource will be the right choice.
> 2) if the attachment is store in binary format that
> is not in the database,
>how to i do that ?
> st, the file send from out from the standalone
> application that store in an object as byte[],
> i will store in an object later pass to sendMail()
> method
> bean.setAttachment(byteBuffer.toByteArray());
> if i am store the file in byte[],
> can i using this
> DataSource ds=new
> ByteArrayDataSource(bs,"application/x-any");
That should work.
# 4
String id=bean.getAttachmentId(); DataSource ds=new DatabaseDataSource(id); although i import javax.activation.DataSource;it doesnt work....pls adviceErr msg :DatabaseDataSource cannot be resolve or it is not a type .
# 5
> String id=bean.getAttachmentId();
> DataSource ds=new DatabaseDataSource(id);
>
> although i import javax.activation.DataSource;
>
> it doesnt work....pls advice
>
>
> Err msg :DatabaseDataSource cannot be resolve or it
> is not a type .
Sorry, maybe I did not explain in clearly enough. As far as I know, there is no class that can work with ByteArrays directly. You have to create the class DatabaseDataSource that implements javax.activation.DataSource.
A simple implemtation would be
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
/**
* @author chufgard
*/
public class ByteArrayDataSource implements DataSource
{
private byte[] data;
private String name;
private String contentType;
public ByteArrayDataSource(byte[] data, String name, String contentType)
{
this.data = data;
this.name = name;
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public InputStream getInputStream() throws IOException
{
return new ByteArrayInputStream(data);
}
public String getName() {
return name;
}
public OutputStream getOutputStream() throws IOException {
throw new IOException(this.getClass().getName()+" does not suppoert write access");
}
}
# 6
javax.mail.util.ByteArrayDataSource