Link to home
Start Free TrialLog in
Avatar of rzvika2
rzvika2

asked on

sending mail with attachment

Hi!
Can you help me with how to send using javax.mail (I'm using the spring of environment, but I'm not sure it matters) a mail with attachments.
I also want to have the ability so save the message (and its attached files) as a one byte array.
Thanks!
SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

Previous is through socket programming whereas this is web mailing to send attachments

http://www.developer.com/java/other/article.php/618471

Best Regards
Sudhakar
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> I also want to have the ability so save the message (and its attached files) as a one byte array.

Do you have access to the mail server code?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rzvika2

ASKER

No, but you can assume that I create the message and know the file names and path.
I assume I can use serialization, but I don't want to...
> I also want to have the ability so save the message (and its attached files) as a one byte array.

I might have misunderstood your question, you obviously mean to save the message just before it leaves *your* mail server, don't you? Or do you mean to save the message on the receiving end mail server?
Avatar of rzvika2

ASKER

I have the MimeMessage object, for example, and I'm adding to it the MimeMultipart.
Now I want to save it to the DB as a byte array (BLOB) for persistency.
Ah I see, I thought you wanted to save it onto the hard disk. Are you using a prepared statement?
Avatar of rzvika2

ASKER

the bottom line is that I need it as a byte array, the DB issue is not relevant.
Yes you do, you need a ByteArrayInputSTream, I just asked for a ps in order to post some sample code. I am assuming you indeed have a ps, then you can do:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
out = new ObjectOutputStream(<your multipart object here>) ;
out.writeObject(object);
out.close();
byte [] bytes = bos.toByteArray();

ps = con.prepareStatement("insert into mytable values ?");
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ps.setBinaryStream(1, bais, bytes.length)
Thank you for accepting :)

BTW I have some mistakes in my code, it should be:

ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
out = new ObjectOutputStream(bos) ;
out.writeObject(<your multipart object here>);
out.close();
Avatar of rzvika2

ASKER

You're welcome, I understood the idea... ;-)